100 likes | 353 Views
Lecture 17. Model-View-Controller. Mother of all Composites: MVC. More than Observer : Model is an Observable and View registers as an Observer More than Strategy : Controller is the Strategy, get another behavior by getting another controller
E N D
Lecture 17 Model-View-Controller
Mother of all Composites: MVC • More than Observer: Model is an Observable and View registers as an Observer • More than Strategy: Controller is the Strategy, get another behavior by getting another controller • More than Composite: View is a Composite of components and GUI elements
How Does It Work? • Model uses the Observer Pattern to keep Views and Controllers informed about its internal state changes • View and Controller implement the Strategy Pattern • Controller is the Concrete Strategy that shares the common Strategy interface: if you want different behavior as reaction to a state change, then use another controller. • View manages the GUI as a Composite
Controller Strategy user did something change your state change your display Observer I’ve changed I need your state info Model View Composite
Example:Account System • An AccountController gets input from a JTextField. User clicks a JButton to withdraw or deposit. AccountController modifies Account to execute the transaction • Account is an Observable that plays the role of Model • When AccountController withdraws or deposits Account notifies each view that the account info has changed • Use three views: • Text • Bar graph • Pie chart
The Class Account import java.util.Observable; public class Account extends Observable{ private double balance; private String name; public Account(String accountName, double deposit){...} private void setBalance(double accountBalance){ balance = accountBalance; setChanged(); notifyObservers(); } ... }
View:AssetPieChart public class AssetPieChartView extends Jpanel implements Observer{ private List accounts=new ArrayList();... public void addAccount(Account account){ ... accounts.add(account);... account.addObserver(this); repaint(); } public void removeAccount(Account account){ account.deleteObserver(this); accounts.remove(account); ... repaint(); public void update(Observable observable, Object object){ repaint();}
The Controller: AccountController public class AccountController extends JPanel{ private Account account; //account controlled public AccountController(Account controlledAccount) { ... account = controlledAccount; JButton depositButton=new JButton(“Deposit”); depositButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ try{ account.deposit(Double.pareseDouble( amountTextField.getText())); } catch .... } .... Also: same for withdrawButton
Manage Views: AbstractAccountView public abstract class AbstractAccountView extends JPanel implements Observer{ private Account account; public AbstractAccountView(Account observableAccount) ...{ account = observableAccount; //account to observe account.addObserver(this); ...} protected abstract void updateDisplay(); public void update(Observable observable, Object object){ updateDisplay(); } }