OOP Design Patterns Chapters 13-16 Design Patterns • The main idea behind design patterns is to extract the high level interactions between objects and reuse their behaviors from application to application. • They were inspired by an architect, Christopher Alexander, discussing the way that architectural design has patterns of its own: – Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. A Timeless Way of Building MVC Design Patterns • Model-View-Controller (MVC) • A collection of simpler patterns that are incredibly useful. • Using MVC, you think of the application in terms of these three modules – Model : The core of the application. This maintains the state and data that the application represents. When significant changes occur in the model, it notifies all of its views – Controller : The user interface presented to the user to manipulate the application. – View : The user interface which displays information about the model to the user. Any object that needs information about the model needs to be a registered view with the model. MVC Advantages • MVC decouples the model, view, and controller from each other to increase flexibility and reuse. – You can attach multiple views to the model without rewriting it. – You can change the way a view responds to user input without changing the visual presentation. For example, you might use a pop-up menu instead of keyboard command keys. MVC Design Patterns • MVC decouples views and model by establishing a subscribe/notify protocol which is an example of the Observer design pattern. Observer – Problem: How do you allow two or more independent and loosely coupled objects to change in synchrony with each other? – Solution: Maintain a list of objects that are tied, or dependent, on another object. When the target object changes, the dependents, or observers, are notified that they should update themselves. Observer Design Pattern • Examples: – User interface components that permit interaction (buttons, scrollbar, etc.) maintain a collection of listener objects. – We can dynamically add or remove listener objects as long as they satisfy an interface. – When the state of the GUI component changes, each of the listeners is notified of a change. Observer Design Pattern • The class library Observable represents objects that can be “observed”. – the equivalent of the GUI component in AWT – What would be observed in the MVC architecture? • Objects wishing to be observed can either – subclass Observable, or – have an Observable instance variable • Other objects can implement the Observer interface which correspond to the listeners • An Observer registers itself with the object being observed. • The Observable object invokes notifyObservers() at any time to indicate that it has changed state. • notifyObservers() causes each observer to be sent a message update(Observable, Object), where – first argument is the observable that changed – second argument is optional, but usually provides Observer Design Pattern • Look at code for HeartBeat example MVC Design Patterns • MVC also lets you change the way a view responds to user input without changing its visual presentation – A view uses an instance of a Controller subclass to implement a particular response strategy – To implement a different strategy, simply replace the instance with a different kind of controller • e.g., a view can be “disabled” so that it does not accept input simply by giving it a controller that ignores input events – The view-controller relationship is an example of the Strategy design pattern. Strategy Design Pattern • Problem: How do you allow the algorithm that is used to solve a particular problem to be easily and dynamically changed by the client? • Solution: Define a family of algorithms with a similar interface. Each algorithm provides a different strategy for solving the problem at hand. Encapsulate each algorithm, and let the client select the strategy to be used in any situation. Strategy Design Pattern • Example: the creation of layout managers in the AWT – rather that coding in the component library (e.g., frame, panel, etc.) the details of how items are laid out on the screen, these decisions are left to the layout manager. – an interface for LayoutManager is defined – the activities of the GUI components (e.g., frame, panel, etc.) are independent of the layout manager being used. Container inherits Application holds LayoutManager implements GridLayout MVC Design Patterns • MVC – views can be nested • Nested views is supported with the CompositeView class which is a subclass of the View class • CompositeView objects act just like View objects, except they also contain and manage nested views.