Java Reference
In-Depth Information
controller.notifyAddFunds(Double.parseDouble(add.getText()));
}
}
Finally, your controller should be reworked:
import javax.swing.JFrame;
public class AccountController {
private Account model;
public AccountController(Account model, AccountWindow view) {
this.model = model;
}
public static void main(String args[]) {
Account myAccount = new Account("AimeeBartSeppe Ltd.", 3000);
AccountWindow myView = new AccountWindow();
AccountController controller = new AccountController(myAccount, myView);
// Register controller and view
// (This can also be done in controller constructor)
myView.registerController(controller);
myAccount.addAccountListener(myView);
myView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myView.setVisible(true);
}
public void notifyAddFunds(double amount) {
model.addFunds(amount);
}
}
Note that, technically, the controller does not need to be aware of the view anymore, so you can
remove the view field from the controller class. You can also remove it from the constructor, but
depending on your preference, you can also let the controller handle its registration and register the
view in the model, so that you can decide to keep it in.
When you run this application, you'll see that it behaves exactly the same as before.
NOTe Well. . . almost exactly. Notice that the funds field stays empty until you
add some funds. How would you solve this issue while adhering to the model‐
view‐controller principles? Purists would argue that it would be best to add an
notifyControllerRegistered method to the controller that can be called by
the view. The controller would then call a refreshListeners method in the
continues
Search WWH ::




Custom Search