The solution I came up with does not really affect the title of the question, but is great for my needs. Because:
- In my case, the same functionality is always executed (for example, the onConfirm function), and this functionality is required in each instance.
- Each instance requiring unique functionality really did not match the factory pattern.
I made my GUI element element:
abstract class Element extends JPanel { ... private final JButton button = new JButton("OK"); protected abstract void onConfirm(); public Element(){ this.button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { onConfirm(); } }); ... } }
With a few anonymous implementations of the inner class that allow me to access the internal elements of the GUI.
this.getContentPane().add(new Element(){ @Override public void onConfirm() { doStuffWithCommand(); } });
source share