Model View Controller Pattern Patterns Model View Controller Folding Intra Class Pattern Loan int new ALoan 0 1 T result I T nextValue getNextValue while isSentinel nextValue result f result nextValue nextValue getNextValue ALoan add 0 A Single Class Scanning Inter Class Pattern Scanner Instance Enumeration Interface Scanner User public interface Type Enumeration public Type nextElement public boolean hasMoreElements Architectural Pattern Christopher 79 Each pattern is a three part rule which expresses a relation between a certain context a problem and a solution Each pattern describes a problem which occurs over and over again in our environment and then describes the core of a solution to that problem in such a way that you can use this solution a million times over Book on Design Patterns MVC Pattern Controller AConsoleController instance View AConsoleView instance Controller AnAWTController instance View ABarChartView instance Controller AnAWTController instance Controller ASwingController instance Model APointHistoryModel instance View APlotterView instance View Aplotterview instance Displaying Points Displaying Points AWT button Swing button Swing Detachable Toolbar Displaying Points All four views displayed concurrently and kept in Sync ObjectEditor based Structure APointHistory ObjectEditor Reusing Point History APointHistory User Interface Code Single Main Class More Modular Approach Editor AConsoleEditor APointHistory Editor ABarChartEditor Editor AnAWTPlotterEditor Editor ASwingPlotterEditor Composer Single Main Class Sharing the View Sharing the Controller Controller Without View View Without Controller Monolithic Editor AWT Plotter Editor APointHistory Separate View and Controller Performs Input Performs Output Write Read Controller AnAWTController methods methods addElement View ABarchartView size elementAt APointHistory Multiple Views and Controllers Controller AConsoleController View AConsoleView Controller AnAWTController View ABarChartView APointHistory Controller AnAWTController View APlotterView Controller ASwingController View APlotterView Syncing Controllers and Views Model View Controller Framework Controller AConsoleController Controller AnAWTController Listeners Listenable Model APointHistoryModel Controller AnAWTController Controller ASwingController View AConsoleView View ABarChartView View APlotterView Changed object notifies views View APlotterView Model View Controller Framework Controller AConsoleController Controller AnAWTController Composer Single Main Class Model APointHistoryModel View AConsoleView View ABarChartView Controller AnAWTController View APlotterView Controller ASwingController View APlotterView Composer connects models views controllers Class Vs Instance Controller AConsoleController Controller AnAWTController Composer Single Main Class Model APointHistoryModel Controller AnAWTController Controller ASwingController View AConsoleView View ABarChartView View APlotterView Different State View APlotterView MVC Framework Controller AConsoleController instance Controller AnAWTController instance Composer Single Main Class Model APointHistoryModel Instance View AConsoleView instance View ABarChartView instance Controller AnAWTController instance View APlotterView instance Controller ASwingController instance View APlotterView instance Model Controller AConsoleController instance Controller AnAWTController instance Composer Single Main Class Model APointHistoryModel Instance View AConsoleView instance View ABarChartView instance Controller AnAWTController instance View APlotterView instance Controller ASwingController instance View APlotterView instance Model Vs PointHistory Controller AConsoleController instance Controller AnAWTController instance Composer Single Main Class APointHistory Instance View AConsoleView instance View ABarChartView instance Controller AnAWTController instance View APlotterView instance Controller ASwingController instance View APlotterView instance Reusing PointHistory IMPLEMENTS PointHistory EXTENDS EXTENDS IMPLEMENTS PointModel APointHistory APointHistory Model Notification Scheme View AConsoleView instance Each view is registered with model Each write method in model calls a notification method in each view Notification method updates display Model APointHistoryModel Instance Each student is registered with professor s listserv Model When web page is updated PointModel Instance Animating mail sent to students Point Student reads web page View ABarChartView instance View APlotterView instance View APlotterView instance View with Multiple Models PointModel Instance APointHistoryModel Instance General Notification Scheme View AConsoleView instance View may have multiple models with common notification method Notification method Model parameter indicates which APointHistoryModel model Instance Model PointModel Instance Animating Point View ABarChartView instance View APlotterView instance View APlotterView instance PointHistoryModel PointHistoryListener View console barchart plotter public interface PointHistoryModel extends PointHistory public void addListener PointHistoryListener pointHistoryListener public void removeListener PointHistoryListener pointHistoryListener Common interface of all views public interface PointHistoryListener public void pointHistoryUpdated PointHistory pointHistory Called whenever model is updated Updated object model APointHistoryModel Each write method notifies import java util Vector import java util Enumeration public class APointHistoryModel extends APointHistory implements PointHistoryModel public void addElement int x int y super addElement x y notifyListeners Vector listeners new Vector public void addListener PointHistoryListener pointHistoryListener listeners addElement pointHistoryListener public void removeListener PointHistoryListener pointHistoryListener listeners removeElement pointHistoryListener void notifyListeners Enumeration elements listeners elements while elements hasMoreElements Object evaluating PointHistoryListener expression Object on elements nextElement pointHistoryUpdated this which enclosing method executed Console View import shapes PointModel public class APointHistoryConsoleView implements PointHistoryListener public void pointHistoryUpdated PointHistory pointHistory System out println for int i 0 i pointHistory size i PointModel nextPoint PointModel pointHistory elementAt i System out println nextPoint getX nextPoint getY System out println
View Full Document