jeudi 5 mars 2015

What design pattern should to affect a specific object with an ActionListener



I got two JButton that implements the same ActionListener.


This ActionListener need to call a function on my other object which i will call container just for the sake of this question.


Because two classes uses ActionListener I made it a outer-class.


Just to give a context, button1 is a JMenuItem and button2 is a JButton on a JToolBar uses my ActionListener and are not directly linked to container. container is a JPanel in a JPanel in a Jpanel.


I thought to register container to my ActionListener, just like i would do with an Observer pattern and keep an ArrayList containing, but i'm not sure if it is the good approach.


I've read about the Visitor pattern, but honestly i don't quite see the difference with the Observer pattern.


What i understand from a Bridge pattern is that i decouple the object so two distinct object can evolve indepently from each other.


Is there another approach I did'nt explore that would be better?


I find the issue confusing so my question is probably confusing as well. Let me know if you need more specificity.


-------------EDIT----------------------



public class Main {

static MyFrame f;

public static void main(String[] args) {

f = new MyFrame();
f.setVisible(true);
}

public class MyFrame extends JFrame {

public Frame() {
this.setSize(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
this.add(new PaneOne(), BorderLayout.WEST);
this.add(new PaneTwo(), BorderLayout.EAST);
this.add(new Container(), BorderLayout.CENTER);
}
}

public class PaneOne extends JPanel {

JButton button1 = new JButton("One");

public PaneOne() {
button1.addActionListener(new Action());
this.add(button1);
}
}

public class PaneTwo extends JPanel {

JButton button2 = new JButton("two");

public PaneTwo() {
button2.addActionListener(new Action());
this.add(button2);
}
}

public class Container extends JPanel {
JLabel label = new JLabel("Before");
public Container() {
this.add(label);
}
}

public class Action implements ActionListener {

@Override
public void actionPerformed(ActionEvent arg0) {
//I want to change the content of label in Container
//I really got no clue on how to do it

}

}
}


Here is a VERY VERY VERY simplified example. My code got more "depthness" in it. My Container is more deeper so a direct reference would be to complicated.


I might be over-engeneering this, but i got no clue on how to link those classes together.


My first solution was to make my ActionListener as a Singleton and link it like I would do with a Observer pattern.


------------------EDIT2------------------- This code look a bit more like my actual design. The ActionListener is a Singleton and need my container to register to it.


Sorry for the confusion, but this issue really make me wonder how things work.



public class Main {

static MyFrame f;

public static void main(String[] args) {

f = new MyFrame();
f.setVisible(true);
}

public class MyFrame extends JFrame {

public MyFrame() {
this.setSize(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
this.add(new PaneOne(), BorderLayout.WEST);
this.add(new PaneTwo(), BorderLayout.EAST);
this.add(new Container(), BorderLayout.CENTER);
}
}

public class PaneOne extends JPanel {

JButton button1 = new JButton("One");

public PaneOne() {
button1.addActionListener(Action.getInstance());
this.add(button1);
}
}

public class PaneTwo extends JPanel {

JButton button2 = new JButton("two");

public PaneTwo() {
button2.addActionListener(Action.getInstance());
this.add(button2);
}
}

public class Container extends JPanel implements Observer {
JLabel label = new JLabel("Before");
Action action = Action.getInstance();
public Container() {
action.register(this);
this.add(label);
}
}

public class Action implements ActionListener {

Container observer;

static Action action;

protected Action() {

}

public static Action getInstance() {
if(action == null) {
action = new Action();
}
return action;
}

public void register(Observer o) {
observer = o;
}

@Override
public void actionPerformed(ActionEvent arg0) {
observer.label.setText("After");

}

}

public interface Observer {
public void update();
}


}




Aucun commentaire:

Enregistrer un commentaire