Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MVC and Other Design Patterns

4,975 views

Published on

This is Class 6 on a 6 week course I taught on Software Design Patterns.

This course provides an introduction into the MVC patterns and briefly goes over other patterns not covered in detail in the class.

Class based on "Head First Design Patterns."

  • These are great slides! I will use then in my OOAD class
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

MVC and Other Design Patterns

  1. 1. Design Patterns 05/28/10 Week 6: MVC and Other Patterns Jonathan Simon [email_address]
  2. 2. DJView <ul><li>Pg 534 </li></ul><ul><li>Two Different User Interfaces </li></ul><ul><ul><li>View – Shows BPM and Beat in real time </li></ul></ul><ul><ul><li>Control – Enter BPM and click start button. Increase and Decrease buttons. </li></ul></ul><ul><li>Pg 535 </li></ul><ul><ul><li>Start and Stop buttons </li></ul></ul>05/28/10
  3. 3. DJView: Version 1 <ul><li>//Fields </li></ul><ul><li>int bpm; </li></ul><ul><li>Sequencer sequencer; </li></ul><ul><li>public DJView() { </li></ul><ul><li>//Display UI Elements </li></ul><ul><li>CreateDisplayView(); </li></ul><ul><li>CreateControlView(); </li></ul><ul><li>//Enable/Disable UI State </li></ul><ul><li>DisableStopMenuItem(); </li></ul><ul><li>EnableStartMenuItem(); </li></ul><ul><li>sequencer = new Sequencer(); </li></ul><ul><li>} </li></ul>05/28/10
  4. 4. DJView: Version 1 <ul><li>public void StartButtonPushed() { </li></ul><ul><li>sequencer.start(); </li></ul><ul><li>bpm = 90; </li></ul><ul><li>sequencer.setTempoInBpm(bpm); </li></ul><ul><li>DisableStartMenuItem(); </li></ul><ul><li>EnableStopMenuItem(); </li></ul><ul><li>DisplayBpm(bpm); </li></ul><ul><li>} </li></ul><ul><li>public void StopButtonPushed() { </li></ul><ul><li>sequencer.turnOff(); </li></ul><ul><li>bpm = 0; </li></ul><ul><li>EnableStartMenuItem(); </li></ul><ul><li>DisableStopMenuItem(); </li></ul><ul><li>DisplayBpm(bpm); </li></ul><ul><li>} </li></ul>
  5. 5. DJView: Version 1 <ul><li>public void SetBpm(int bpm) { </li></ul><ul><li> this.bpm = bpm; </li></ul><ul><li>sequencer.setTempoInBpm(bpm); </li></ul><ul><li> DisplayBpm(bpm); </li></ul><ul><li>} </li></ul><ul><li>public void IncreaseBpm() { </li></ul><ul><li> this.bpm = bpm + 1; </li></ul><ul><li> sequencer.setTempoInBpm(bpm); </li></ul><ul><li> DisplayBpm(bpm); </li></ul><ul><li>} </li></ul><ul><li>public void DecreaseBpm() { </li></ul><ul><li>this.bpm = bpm - 1; </li></ul><ul><li>sequencer.setTempoInBpm(bpm); </li></ul><ul><li>DisplayBpm(bpm); </li></ul><ul><li>} </li></ul>
  6. 6. DJView: Version 1 <ul><li>public void UpdateBeat() { </li></ul><ul><ul><li>while (true) { </li></ul></ul><ul><ul><li>if (sequencer.BeatChanged) </li></ul></ul><ul><ul><li>DisplayBeat(sequencer.getBeat()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li> </li></ul>05/28/10
  7. 7. DJView: Version 1 05/28/10
  8. 8. Requirement Change… <ul><li>Now we need another screen (ArtistView) to display BPM. (as well other information just as Song Name, Artist, Album Name, year, etc). </li></ul><ul><ul><li>Note: ArtistView is not interested in a change in Beat. </li></ul></ul><ul><li>How can we display BPM? </li></ul><ul><ul><li>We can modify function DisplayBpm() to update ArtistView and DJView whenever a change is made to BPM. </li></ul></ul><ul><ul><li>What happens when we create another screen, say AlbumView? </li></ul></ul>05/28/10
  9. 9. So.. <ul><li>We have a few different screens (DJView, ArtistView, AlbumView) that are all interested whenever we have a change in data state (BPM or Beat). </li></ul><ul><ul><li>Some screens are interested in change in BPM, others change Beat, and some changes in both. </li></ul></ul><ul><li>Currently, we have to update DJView whenever we want to add another screen that is interested in changes in BPM or Beat. </li></ul><ul><li>What pattern is screaming to be used? </li></ul>05/28/10
  10. 10. Creating a Subject <ul><li>We need a Subject that contains the data state that we know is changing. </li></ul><ul><li>In this example, what should fields should go in our Subject? </li></ul><ul><li>How many different events should our Subject expose? </li></ul><ul><li>How do we “glue” our Observers to this Subject? </li></ul>05/28/10
  11. 11. Subject = BeatModel <ul><li>public class BeatModel { </li></ul><ul><li>int bpm; </li></ul><ul><li>Sequencer sequencer; </li></ul><ul><li>public void initialize() { </li></ul><ul><li>sequencer = new Sequencer(); </li></ul><ul><li>} </li></ul><ul><li>public void RegisterObserver(BeatObserver o) { } </li></ul><ul><li>public void RemoveObserver(BeatObserver o) { } </li></ul><ul><li>public void NotifyBeatObservers() { } </li></ul><ul><li>public void RegisterObserver(BPMObserver o) { } </li></ul><ul><li>public void RemoveObserver(BPMObserver o) { } </li></ul><ul><li>public void NotifyBpmObservers() { } </li></ul><ul><li>} </li></ul>
  12. 12. BeatObserver and BPMObserver <ul><li>public interface BeatObserver { </li></ul><ul><li>void updateBeat(); </li></ul><ul><li>} </li></ul><ul><li>public interface BPMObserver { </li></ul><ul><li>void updateBPM(); </li></ul><ul><li>} </li></ul>05/28/10
  13. 13. DJView: Version 2 <ul><li>//Fields </li></ul><ul><li>int bpm; </li></ul><ul><li>Sequencer sequencer; </li></ul><ul><li>public DJView() { </li></ul><ul><li>//Display UI Elements </li></ul><ul><li>CreateDisplayView(); </li></ul><ul><li>CreateControlView(); </li></ul><ul><li>//Enable/Disable UI State </li></ul><ul><li>DisableStopMenuItem(); </li></ul><ul><li>EnableStartMenuItem(); </li></ul><ul><li>sequencer = new Sequencer(); </li></ul><ul><li>} </li></ul>05/28/10 Changes???
  14. 14. DJView: Version 2 <ul><li>//Fields </li></ul><ul><li>BeatModel model ; </li></ul><ul><li>public DJView() { </li></ul><ul><li>//Display UI Elements </li></ul><ul><li>CreateDisplayView(); </li></ul><ul><li>CreateControlView(); </li></ul><ul><li>//Enable/Disable UI State </li></ul><ul><li>DisableStopMenuItem(); </li></ul><ul><li>EnableStartMenuItem(); </li></ul><ul><li>model = new BeatModel(); </li></ul><ul><li>model.initialize(); </li></ul><ul><li>} </li></ul>05/28/10
  15. 15. DJView: Version 2 <ul><li>public void StartButtonPushed() { </li></ul><ul><li>sequencer.start(); </li></ul><ul><li>bpm = 90; </li></ul><ul><li>sequencer.setTempoInBpm(bpm); </li></ul><ul><li>DisableStartMenuItem(); </li></ul><ul><li>EnableStopMenuItem(); </li></ul><ul><li>DisplayBpm(bpm); </li></ul><ul><li>} </li></ul><ul><li>public void StopButtonPushed() { </li></ul><ul><li>sequencer.turnOff(); </li></ul><ul><li>bpm = 0; </li></ul><ul><li>EnableStartMenuItem(); </li></ul><ul><li>DisableStopMenuItem(); </li></ul><ul><li>DisplayBpm(bpm); </li></ul><ul><li>} </li></ul>Does DJView have a direct reference : To sequencer? To bpm field?
  16. 16. DJView: Version 2 <ul><li>public void StartButtonPushed() { </li></ul><ul><li>model.on(); </li></ul><ul><li>DisableStartMenuItem(); </li></ul><ul><li>EnableStopMenuItem(); </li></ul><ul><li>DisplayBpm(bpm); </li></ul><ul><li>} </li></ul><ul><li>public void StopButtonPushed() { </li></ul><ul><li>model.off(); </li></ul><ul><li>EnableStartMenuItem(); </li></ul><ul><li>DisableStopMenuItem(); </li></ul><ul><li>DisplayBpm(bpm); </li></ul><ul><li>} </li></ul>05/28/10 What should we do with this DisplayBpm function? What interfaces does DJView need to implement? Remember BeatModel…
  17. 17. DJView: Version 2 <ul><li>public class DJView : BeatObserver, BPMObserver { </li></ul><ul><li>public void updateBeat() { </li></ul><ul><li>//Display change in Beat in the UI </li></ul><ul><li>} </li></ul><ul><li>public void updateBpm() { </li></ul><ul><li>int bpm = model.getBpm(); </li></ul><ul><li>if (bpm == 0) </li></ul><ul><li> //Display &quot;offline&quot; </li></ul><ul><li>else </li></ul><ul><li>//Display &quot;Current BPM&quot; with value </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 What are we missing?
  18. 18. DJView: Version2 <ul><li>public DJView() { </li></ul><ul><li> //Display UI Elements </li></ul><ul><li>CreateDisplayView(); </li></ul><ul><li>CreateControlView(); </li></ul><ul><li>//Enable/Disable UI State </li></ul><ul><li>DisableStopMenuItem(); </li></ul><ul><li>EnableStartMenuItem(); </li></ul><ul><li>model = new BeatModel(); </li></ul><ul><li>model.initialize(); </li></ul><ul><li>model.RegisterObserver((BeatObserver)this); </li></ul><ul><li>model.RegisterObserver((BPMObserver)this); </li></ul><ul><li>} </li></ul>05/28/10
  19. 19. DJView: Version 2 <ul><li>public void StartButtonPushed() { </li></ul><ul><li>model.on(); </li></ul><ul><li>DisableStartMenuItem(); </li></ul><ul><li>EnableStopMenuItem(); </li></ul><ul><li>} </li></ul><ul><li>public void StopButtonPushed() { </li></ul><ul><li>model.off(); </li></ul><ul><li>EnableStartMenuItem(); </li></ul><ul><li>DisableStopMenuItem(); </li></ul><ul><li>} </li></ul><ul><li>public void SetBpm(int bpm) { </li></ul><ul><li>model.setBPM(bpm); </li></ul><ul><li>} </li></ul><ul><li>public void IncreaseBpm() { </li></ul><ul><li>model.increaseBpm(); </li></ul><ul><li>} </li></ul><ul><li>public void DecreaseBpm() { </li></ul><ul><li>model.decreaseBpm(); </li></ul><ul><li>} </li></ul>
  20. 20. Version 2 05/28/10 Notice beatObservers and bpmObservers in BeatModel.
  21. 21. View and Model <ul><li>We now have a View and a Model </li></ul><ul><li>The View is also an Observer. It is interested in changes in the Model. </li></ul><ul><li>When the Model has a data change, the View gets notified (updateBPM). The View then gets data from the Model (getBPM). </li></ul><ul><li>We can have multiple Views all interested in this Model. </li></ul>05/28/10
  22. 22. Multiple Views w/One Model 05/28/10 Question: Does the Model know any specific details about the UI elements on the View?
  23. 23. Definition (Partial): Model <ul><li>Contains the data, state, and application logic. </li></ul><ul><li>State changes can occur internally or externally. (more on this later). </li></ul><ul><li>When a change of state occurs, the Model sends a notification message. </li></ul><ul><li>The Model doesn’t have any direct references to the Views that need it. (Since the Views registers themselves with the Model). </li></ul>05/28/10
  24. 24. Definition (Partial): View <ul><li>Graphic User Interface. </li></ul><ul><li>It is the “window” to the model. </li></ul><ul><li>View registers with the Model for notification of certain events. </li></ul><ul><li>When notified by the Model, the View asks the Model for state information. </li></ul><ul><li>View must have a direct reference to the Model in order to get data. (can also be an interface the Model uses) </li></ul>05/28/10
  25. 25. Requirement Change <ul><li>We need to change all of these views from a Windows application to a Web application. </li></ul><ul><li>We would like to minimize the amount of code that we need to re-write for the new user interface. </li></ul><ul><li>Let’s look at the DJView code again.. </li></ul>05/28/10
  26. 26. DJView: Version 2 <ul><li>public DJView() { </li></ul><ul><li>CreateDisplayView(); </li></ul><ul><li>CreateControlView(); </li></ul><ul><li>DisableStopMenuItem(); </li></ul><ul><li>EnableStartMenuItem(); </li></ul><ul><li>//Hidden: Create model, initialize and register observers </li></ul><ul><li>} </li></ul><ul><li>public void StartButtonPushed() { </li></ul><ul><li>model.on(); </li></ul><ul><li>DisableStartMenuItem(); </li></ul><ul><li>EnableStopMenuItem(); </li></ul><ul><li>} </li></ul><ul><li>public void updateBeat() { ... } </li></ul><ul><li>public void updateBPM() { ... } </li></ul>We know that we cannot re-use UI elements when we change Views.
  27. 27. Challenge <ul><li>We want to separate elements from DJView that vary from the things that do not vary. </li></ul><ul><li>What varies? </li></ul><ul><li>What does not vary? </li></ul>05/28/10
  28. 28. <ul><li>What Varies </li></ul><ul><ul><li>Internals of CreateDisplayView() + CreateControlView() </li></ul></ul><ul><ul><li>Internals of all of the Disable/Enable methods </li></ul></ul><ul><ul><li>The internals of the updateBeat and updateBPM that has to do with updating UI </li></ul></ul><ul><ul><li>How events are associated with UI element </li></ul></ul><ul><li>What does not vary </li></ul><ul><ul><li>The sequence of events. For example, StartButtonPushed has a specific set of steps. </li></ul></ul><ul><ul><li>Calling the Model to do something (on(), off()) </li></ul></ul><ul><ul><li>Registering View with the Model </li></ul></ul><ul><ul><li>The internals of the updateBPM that has to do with getting data from the Model </li></ul></ul>05/28/10
  29. 29. Inheritance <ul><li>Base View class that contains: </li></ul><ul><ul><li>Reference to BeatModel </li></ul></ul><ul><ul><li>Template Pattern to define known set of steps; use abstract methods for pieces that need to be implemented by sub class. </li></ul></ul><ul><li>Each subclass just defines the UI specific elements. </li></ul><ul><li>What problems will we encounter? </li></ul>05/28/10
  30. 30. Inheritance (cont) <ul><li>We can’t inherit form the base View and the specific UI form (which contains a lot of functionality for interacting with UI elements) </li></ul><ul><li>We would need to inherit from the Base View and wrap the specific UI element (Windows or Web form) and then expose what we need. (Adapter Pattern!) </li></ul><ul><li>This would be painful… </li></ul>05/28/10
  31. 31. Composition <ul><li>View just contains the UI elements and functions that manipulate the UI elements ( What Varies ) </li></ul><ul><li>View associates UI element with a handler. ( What Varies ) </li></ul><ul><li>The UI handler may do a special sequence of events (which may manipulate a combination of model changes and UI updates). This is What Not Varies . </li></ul><ul><li>Here is our separation of View and Controller (the New Guy). </li></ul>05/28/10
  32. 32. What about the Model? <ul><li>The View interacts with the Model in two ways: </li></ul><ul><li>Sends a message to the Controller to update the Model. (setBPM, increase, decrease) </li></ul><ul><li>Gets notified when Model changes (updateBeat, updateBPM) </li></ul>05/28/10
  33. 33. DJView: Version 3 (Pt 1) <ul><li>public class DJView : BeatObserver, BPMObserver { </li></ul><ul><li>BeatModel model; </li></ul><ul><li>BeatController controller; </li></ul><ul><li>public DJView(BeatModel model, BeatController controller) </li></ul><ul><li>{ </li></ul><ul><li>this.model = model; </li></ul><ul><li>this.controller = controller; </li></ul><ul><li>model.RegisterObserver((BeatObserver)this); </li></ul><ul><li>model.RegisterObserver((BPMObserver)this); </li></ul><ul><li>} </li></ul><ul><li> public void updateBeat() { . . } </li></ul><ul><li> public void updateBPM() { . . } </li></ul>05/28/10 Note: The only reason that View needs Model is for registering of events and getting state from Model.
  34. 34. DJView: Version 3 (Pt 2) <ul><li>public void StartButtonPushed() { </li></ul><ul><li>controller.start(); </li></ul><ul><li>} </li></ul><ul><li>public void StopButtonPushed() { </li></ul><ul><li>controller.stop(); </li></ul><ul><li>} </li></ul><ul><li>public void SetBpm(int bpm) { </li></ul><ul><li>controller.setBPM(bpm); </li></ul><ul><li>} </li></ul><ul><li>public void IncreaseBpm() { </li></ul><ul><li>controller.increaseBPM(); </li></ul><ul><li>} </li></ul><ul><li>public void DecreaseBpm() { </li></ul><ul><li>controller.decreaseBPM(); </li></ul><ul><li>} </li></ul>05/28/10 The View delegates all actions to the Controller.
  35. 35. DJView: Version 3 (Pt 3) <ul><li>//Lastly, we have all of the functions that directly manipulate </li></ul><ul><li>//UI elements in the View. These functions are called externally </li></ul><ul><li>//by the Controller! </li></ul><ul><li>public void CreateDisplayView() { } </li></ul><ul><li>public void CreateControlView() { } </li></ul><ul><li>public void DisableStopMenuItem() { } </li></ul><ul><li>public void EnableStopMenuItem() { } </li></ul><ul><li>public void DisableStartMenuItem() { } </li></ul><ul><li>public void EnableStartMenuItem() { } </li></ul>05/28/10 NOTE: If we have to port this View from Windows to Web, here is where the crux of our changes will occur.
  36. 36. BeatController <ul><li>public class BeatController { </li></ul><ul><li>BeatModel model; </li></ul><ul><li>DJView view; </li></ul><ul><li>public BeatController(BeatModel model) </li></ul><ul><li>this.model = model; </li></ul><ul><li>view = new DJView(model, this); </li></ul><ul><li>view.CreateDisplayView(); </li></ul><ul><li>view.CreateControlView(); </li></ul><ul><li>view.DisableStopMenuItem(); </li></ul><ul><li>view.EnableStartMenuItem(); </li></ul><ul><li>model.initialize(); </li></ul><ul><li>} </li></ul>Why???
  37. 37. BeatController <ul><li>public void start() { </li></ul><ul><li>model.on(); </li></ul><ul><li>view.DisableStartMenuItem(); </li></ul><ul><li>view.EnableStopMenuItem(); </li></ul><ul><li>} </li></ul><ul><li>public void stop() { </li></ul><ul><li>model.off(); </li></ul><ul><li>view.EnableStartMenuItem(); </li></ul><ul><li>view.DisableStopMenuItem(); </li></ul><ul><li>} </li></ul><ul><li>public void setBPM(int bpm) { </li></ul><ul><li>model.setBPM(bpm); </li></ul><ul><li>} </li></ul>05/28/10 Controller sends a message to Model to update data…and sends a message back to the View. Note: Not showing the Model here. It is exactly the same!
  38. 38. The Big Picture 05/28/10 See diagram on page 530.
  39. 39. Definition: Model <ul><li>Contains the data, state, and application logic. </li></ul><ul><li>State changes can occur internally or externally (by a message from the Controller) </li></ul><ul><li>When a change of state occurs, the Model sends a notification message to the Views that want to be notified. </li></ul><ul><li>The Model doesn’t have any direct references to the Views or Controllers that need it. </li></ul>05/28/10
  40. 40. Definition: View <ul><li>Graphic User Interface. </li></ul><ul><li>It is the “window” to the model. </li></ul><ul><li>View registers with the Model for notification of certain events. When notified by the Model, the View asks the Model for state information. </li></ul><ul><li>View must have a direct reference to the Model in order to get data. (can also be an interface the Model uses) </li></ul><ul><li>View delegates actions to the Controller. </li></ul>05/28/10
  41. 41. Definition: Controller <ul><li>Handles user actions initiated on the View. </li></ul><ul><li>Informs the Model of any data changes. </li></ul><ul><li>When applicable, tells the View to change it’s UI state. </li></ul>05/28/10
  42. 42. Lab: Part I <ul><li>In BeatController.start() function does three things: </li></ul><ul><ul><li>Sends on() message to the Model </li></ul></ul><ul><ul><li>Sends DisableStartMenuItem() message to the View </li></ul></ul><ul><ul><li>Sends EnableStopMenuItem() message to the View. </li></ul></ul><ul><li>Then we get a requirement that the end user needs to change the Mode (Basic or Advanced) at any time. </li></ul><ul><ul><li>If user calls start() during Basic mode, the same functionality occurs. </li></ul></ul><ul><ul><li>If user calls start() during Advanced mode, the Basic mode functionality occurs, but in addition, the UI displays a video of the song. </li></ul></ul><ul><li>What can we do? What pattern can help us? </li></ul>
  43. 43. Lab: Part II <ul><li>Let’s say we need to add permissions. Which object should be modified? (Model, View, Controller) </li></ul><ul><li>Let’s say a change in the Model needs to result in the enabling and disabling of certain UI elements in the View. What can we do? </li></ul>05/28/10
  44. 44. Answer : Part I <ul><li>BeatController can be an abstract class with default implementation for all methods, except start (which can be marked abstract) </li></ul><ul><li>Create two classes that inherit from BeatController (Basic and Advanced). Each class defines the unique behavior of start. </li></ul><ul><li>Associate the View with the appropriate version of BeatController. </li></ul><ul><li>Use Strategy Pattern to change behavior at run time. </li></ul>05/28/10
  45. 45. Answer: Part II <ul><li>Controller. We could easily create a Permission object that is accessed by the Controller. This has the benefit of keeping permission-related code out of the View. </li></ul><ul><li>Two Answers: </li></ul><ul><ul><li>The View can easily respond to the change in the Model and set the appropriate UI elements; however, that is putting more business logic in the View. </li></ul></ul><ul><ul><li>Controller can become an Observer of certain events in the Model (in addition to the View!). If a certain change of state occurs in the Model, the Controller can tell the View which UI elements to change. </li></ul></ul>05/28/10
  46. 46. Testing <ul><li>Testing is a lot easier with MVC. </li></ul><ul><li>Unit Tests can target the Controller and Model. </li></ul><ul><li>It is harder to unit test the View. </li></ul>05/28/10
  47. 47. Frameworks for MVC <ul><li>Spring MVC </li></ul><ul><li>Java Swing </li></ul><ul><li>Windows Presentation Foundation (WPF) </li></ul><ul><li>ASP.NET MVC Framework </li></ul><ul><li>PureMVC </li></ul><ul><li>Bistro </li></ul><ul><li>Apache Struts </li></ul><ul><li>And many more… </li></ul>05/28/10
  48. 48. Model View Presenter (MVP) <ul><li>Derivative of MVC. </li></ul><ul><li>Focuses on the Presentation Layer. For example, interaction of different UI elements on a form. </li></ul><ul><li>References </li></ul><ul><ul><li>http://en.wikipedia.org/wiki/Model-view-presenter </li></ul></ul><ul><ul><li>http://www.mvcsharp.org/ </li></ul></ul>05/28/10
  49. 49. Other Patterns <ul><li>Iterator </li></ul><ul><li>Composite </li></ul><ul><li>Builder </li></ul><ul><li>Façade </li></ul><ul><li>State </li></ul><ul><li>Chain of Responsibility </li></ul><ul><li>Flyweight </li></ul><ul><li>Memento </li></ul>05/28/10
  50. 50. Iterator <ul><li>GoF Intent: “Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” </li></ul><ul><li>There are different types of “aggregate objects” </li></ul><ul><ul><li>Collection </li></ul></ul><ul><ul><li>ArrayList </li></ul></ul><ul><ul><li>List<> </li></ul></ul><ul><ul><li>Array </li></ul></ul><ul><ul><li>string[] </li></ul></ul>05/28/10
  51. 51. Iterator (cont) <ul><li>Each of these objects have different ways of looping through and accessing different elements. </li></ul><ul><li>Iterator patterns hides the implementation of the aggregate object from the clients that access it. </li></ul><ul><li>Advantage: You can change an ArrayList into a List<> without breaking clients that use you it. </li></ul>05/28/10
  52. 52. Composite <ul><li>GoF Intent: “allows you to compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and composition of objects uniformly.” </li></ul><ul><li>Think of a tree hierarchy </li></ul><ul><ul><li>Business objects </li></ul></ul><ul><ul><li>User Interface elements (Window  Panel  Button) </li></ul></ul><ul><li>A Node (with Children) and a Leaf Node are all treated the same. </li></ul>05/28/10
  53. 53. Composite (cont) <ul><li>Example: Windows form where all UI elements are in a tree. Message is sent to top node to “disable all elements”. The tree is traversed and Disable message is applied to each node in the tree. </li></ul><ul><li>Composite and Iterator are often used together. </li></ul>05/28/10
  54. 54. Builder <ul><li>Encapsulate the construction of a product and allow it to be constructed in steps. </li></ul><ul><li>Good for the creation of complex objects. </li></ul><ul><li>Client sends multiple messages to the Builder for creating certain parts of the structure. Then Builder returns Composite result. </li></ul>05/28/10
  55. 55. Facade <ul><li>“ Provides a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use”. </li></ul><ul><li>Example, communication between Business and Data Layer. </li></ul>05/28/10
  56. 56. State Pattern <ul><li>GoF Intent: “Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.” </li></ul><ul><li>Strategy Pattern – behavior change due to an external change. (Remember the setBehavior() method). The Clients had to be aware of the different possible Strategies. </li></ul><ul><li>State is basically like Strategy; however, changes occur internally. </li></ul>05/28/10
  57. 57. Chain of Responsibility <ul><li>“ Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.” </li></ul><ul><li>Each object in chain acts as a handler. If it cannot handle the request, passes it to the next object in the chain. </li></ul><ul><li>Often using in Windows system for handling events generated by keyboard or mouse. </li></ul>05/28/10
  58. 58. Flyweight <ul><li>GoF Intent: “Use sharing to support large numbers of fine-grained objects efficiently.” </li></ul><ul><li>Addresses memory issues. </li></ul><ul><li>Instead of having 100 large objects that take up memory: </li></ul><ul><ul><li>One stateless object </li></ul></ul><ul><ul><li>Manager object that contains the state of the 100 objects </li></ul></ul><ul><li>Single instances will not be able to behave independently from other instances. (Probably better for read-only purposes!) </li></ul>05/28/10
  59. 59. Memento <ul><li>Be able to return object to a previous state. </li></ul><ul><li>Handle undo. </li></ul>05/28/10
  60. 60. Next Steps? <ul><li>Read the rest of the book! </li></ul><ul><li>Buy the Gang of Four book to get more details on the patterns mentioned in this book. </li></ul><ul><li>Understand commonly used frameworks </li></ul><ul><ul><li>MVC </li></ul></ul><ul><ul><li>Spring </li></ul></ul><ul><ul><li>Dependency Injection </li></ul></ul>05/28/10
  61. 61. Martin Fowler <ul><li>http://www.martinfowler.com/ </li></ul><ul><li>Refactoring: Improving the Design of Existing Code </li></ul><ul><li>Patterns of Enterprise Application Architecture </li></ul>05/28/10

×