1 / 10

Lecture 17

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

israel
Download Presentation

Lecture 17

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 17 Model-View-Controller

  2. 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

  3. 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

  4. Controller Strategy user did something change your state change your display Observer I’ve changed I need your state info Model View Composite

  5. 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

  6. User View

  7. 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(); } ... }

  8. 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();}

  9. 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

  10. 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(); } }

More Related