-1

I have a very simple Bank Account going on, that I built to practice my MVC knowledge. However I've hit a snag.

Since the Deposit and Withdraw screens are essentially the same, I abstracted almost all of it to a BaseWindow class.

Deposit Window Withdraw Window

This is the BaseWindow class:

public class BaseWindow extends JFrame {

    private static final long serialVersionUID = 4741220023341218042L;

    JButton executeButton;

    public BaseWindow(String title){
        super(title);
        JPanel panel = new JPanel(new GridLayout(0, 3));
        JLabel amountLabel = new JLabel("Amount: "); 
        JTextField inputBox = new JTextField(15);
        executeButton = new JButton("Execute");
        JLabel newBalanceLabel = new JLabel("New Balance: "); 
        JLabel newBalance = new JLabel();
        panel.add(amountLabel);
        panel.add(inputBox);
        panel.add(executeButton);
        panel.add(newBalanceLabel);
        panel.add(newBalance);
        panel.add(new JLabel()); //Empty, for layout purposes
        add(panel);
        pack();
    }

    public JButton getExecuteButton(){
        return executeButton;
    }
}

The Problem

Since I'm using the Model-View-Controller pattern, the function of the Execute button is determined in the Controller class, which is consists of the following:

...
public void startDepositWindow(){
        DepositWindow depositWindow = view.createDepositWindow();
        depositWindow.getExecuteButton().addActionListener(createExecuteButtonListener());
        depositWindow.run();
    }
    public void startWithdrawWindow(){
        WithdrawWindow withdrawWindow = view.createWithdrawWindow();
        withdrawWindow.getExecuteButton().addActionListener(createExecuteButtonListener());
        withdrawWindow.run();
    }

    //Button Action Listeners
    private ActionListener createDepositButtonListener(){
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                startDepositWindow();
            }
        };
    }
    private ActionListener createWithdrawButtonListener(){
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                startWithdrawWindow();
            }
        };
    }
    private ActionListener createExecuteButtonListener(){
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // WHAT GOES HERE??
            }
        };
    }

...

The problem is, I can't think of what goes in the createExecuteButtonListener because I can't simply say model.deposit() or model.withdraw() because I don't know which window the Execute button was clicked inside of.

As far as I know, I can't use Polymorphism here because the listener is not being bound inside the DepositWindow or WithdrawWindow subclasses and I don't want to call them there since that would violate Model-View-Controller.

So what's bothering me is, how do I tell the button listener/controller what is the right thing to do?

akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • So if I understand correctly, you're using a button (not directly shown in your problem) to create the Withdraw and the Deposit windows? Why are you attaching the same ActionListener, ```createExecuteButtonListener``` to the Execute button? – David Yee Jun 02 '14 at 10:12
  • You got pass the context of the window by parameter to the create listener method, in your case the context is some constant that inform the current window – fmodos Jun 02 '14 at 10:44
  • @CanadianDavid The Deposit and Withdraw windows are subclasses of the BaseWindow class – CodyBugstein Jun 02 '14 at 10:46
  • In your images, they look like they're exactly the same so why do you need to subclass the BaseWindow class when you can just call ```BaseWindow("Deposit")``` and ```BaseWindow("Withdraw")``` and simply use add a separate ActionListener to each BaseWindow's ```getExecuteButton()```? – David Yee Jun 02 '14 at 10:48

1 Answers1

3

Use Action "to separate functionality and state from a component." Let the model export a suitable DepositAction and WithdrawAction. Let the controller associate each action with the correct Execute button. Some related examples are cited here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045