Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-1 PS95&96-MEF-L16-1 Dr. M.E. Fayad Creationa l.

Similar presentations


Presentation on theme: "Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-1 PS95&96-MEF-L16-1 Dr. M.E. Fayad Creationa l."— Presentation transcript:

1 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-1 PS95&96-MEF-L16-1 Dr. M.E. Fayad Creationa l Paradigm Shift, Inc. Software Factory Behaviora l Structural Lesson 9: More On Behavioral Patterns Object-Oriented Design Patterns

2 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-2 PS95&96-MEF-L16-2 Dr. M.E. Fayad Lesson Objectives oPresent the following Patterns: Mediator Observer oDiscuss in detail the behavioral patterns

3 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-3 PS95&96-MEF-L16-3 Dr. M.E. Fayad Behavioral Patterns  Mediator Pattern Topics –Mediator Definition –Structure –Graphic UI Example –Problems it Solves –Participants –Benefits –Related Patterns

4 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-4 PS95&96-MEF-L16-4 Dr. M.E. Fayad Mediator Pattern: Definition Mediator object coordinates communication between interacting objects. Interacting objects work with and through a Mediator rather than each other directly. The Mediator pattern is also known as a “glue” pattern. The Mediator pattern decouples the individual tasks. –Each object only knows its own input and output. –This way, each object does not depend on the other objects around it.

5 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-5 PS95&96-MEF-L16-5 Dr. M.E. Fayad Mediator Pattern: Illustration Client or User Object A Object C Object D Object B The “Before” Model

6 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-6 PS95&96-MEF-L16-6 Dr. M.E. Fayad Mediator Pattern: More Illustration Client or User Object A Object C Object D Object B The “After” Model Mediator Object Abstraction Colleague objects

7 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-7 PS95&96-MEF-L16-7 Dr. M.E. Fayad Mediator Pattern: Structure Mediator colleague classes

8 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-8 PS95&96-MEF-L16-8 Dr. M.E. Fayad Dialog Director Example DialogDirector ShowDialog() CreateControls() HandleChange(Control) Control Select() director-> HandleChange(this) EntryField director ListDialog CreateControls() HandleChange(Control) ListBox DialogDirector is an abstract class that defines the overall infrastructure of a dialog. It keeps a reference to the window that presents the dialog. Clients call the ShowDialog operation to display the dialog on the screen. CreateControls and HandleChange are abstract operations. DialogDirector subclasses override CreateControls to create the proper controls and HandleChange to handle the changes. DialogDirector is an abstract class that defines the overall infrastructure of a dialog. It keeps a reference to the window that presents the dialog. Clients call the ShowDialog operation to display the dialog on the screen. CreateControls and HandleChange are abstract operations. DialogDirector subclasses override CreateControls to create the proper controls and HandleChange to handle the changes.

9 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-9 PS95&96-MEF-L16-9 Dr. M.E. Fayad Dialog Director Example: Interaction Diagram GetSelection DialogDirector Colleague Client SetText HandleChange ListBox ShowDialog Mediator EntryField

10 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-10 PS95&96-MEF-L16-10 Dr. M.E. Fayad Mediator Pattern: Participants & Collaborations Mediator (DialogDirector) –implements cooperative behavior by coordinating Colleague objects. –stores state. Colleague classes (ListBox, EntryField) –implements pieces of the cooperative behavior. Collaborations Colleagues send/receive requests to/from a Mediator object. The Mediator implements the cooperative behavior by routing requests between the appropriate Colleague(s). Collaborations Colleagues send/receive requests to/from a Mediator object. The Mediator implements the cooperative behavior by routing requests between the appropriate Colleague(s). Participants

11 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-11 PS95&96-MEF-L16-11 Dr. M.E. Fayad It is time for breakfast. A customer places an order and gives it to the egg cooker. The egg cooker does his job and then moves the order to the fry cook for meat and hash browns. The fry cook sends the plate on, and so forth. When the order has finished processing in the kitchen the customer’s name is called and the plate is given to the customer. This scheme is inflexible because you cannot change the structure of the kitchen and you cannot add any other steps to the cooking processes. Mediator Pattern: Restaurant Example

12 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-12 PS95&96-MEF-L16-12 Dr. M.E. Fayad Mediator Pattern: Restaurant Example Customer in a restaurant Eggs Toast Fry2 Fry The “Before” Model

13 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-13 PS95&96-MEF-L16-13 Dr. M.E. Fayad Mediator Pattern: The Solution Customer in a Restaurant Eggs Fry2 Toast Fry The “After” Model Waitperson Abstraction Colleague objects

14 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-14 PS95&96-MEF-L16-14 Dr. M.E. Fayad Each of the objects knows only of the Mediator existence. Each performs its task and returns its finished product to the Mediator. Flexibility is added because: –Each individual object is free to change its own methods without effecting the others and without the other’s knowledge. This allows for dynamic binding and polymorphism. –An additional process may be added. This would require a change in the Mediator object. Mediator Pattern: More on The Solution

15 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-15 PS95&96-MEF-L16-15 Dr. M.E. Fayad Mediator Pattern: Advantages Moves complexity out of the problem domain solution (mainline routines). Abstraction hides some non- essential implementation details. Changes or improvements to the underlying functions and objects do not require changes to client code. Frees up individual colleague objects by decoupling them. Enhances the chance for extensibility in the colleague objects since changes to individual colleagues do not ripple through to other colleagues and affect overall dependencies. Different Mediator objects can provide a different set of services to the client by reusing colleague objects.

16 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-16 PS95&96-MEF-L16-16 Dr. M.E. Fayad If the Mediator becomes too complex, extensibility of the Mediator may be limited without significant rework. Do not allow the Mediator to become a “special case” handler. If the degree of complexity that you are moving from the Colleague into the Mediator is small then extra overhead of Mediator development may not be justified. Decision made by the Mediator have to “jive” with what the Mainline routines expect. In other words, “Can you trust your Mediator’s decisions?” Mediator Pattern: Disadvantages

17 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-17 PS95&96-MEF-L16-17 Dr. M.E. Fayad When we have a group of colleagues that interact in complex but well-defined ways. When the order of individual colleague operations might change. Relieve colleague objects from the additional burden of mediation in addition to their regular services. Mediator Pattern: When It Should be Used

18 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-18 PS95&96-MEF-L16-18 Dr. M.E. Fayad Behavioral Patterns  Observer Pattern Topics –Observer Definition –Structure –Application Area –Problems it Solves –Participants –Benefits –Implementation Issues –Discussion Questions

19 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-19 PS95&96-MEF-L16-19 Dr. M.E. Fayad Observer Pattern: Definition Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. It is also known as Publish-Subscribe or Dependents The key objects in the Observer pattern are Subject and Observer. A Subject may have any number of dependent observers. All observers are notified whenever the subject undergoes a change in state. Any number of observers can subscribe to receive notifications.

20 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-20 PS95&96-MEF-L16-20 Dr. M.E. Fayad Observer Pattern: Structure for all o in observers { o->Update() } return subjectState Subject Attach(Observer) Detach(Observer) Notify() Observer Update() ConcreteSubject GetState() observers subjectState observerState = subject->GetState(); ConcreteObserver Update() observerState subject

21 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-21 PS95&96-MEF-L16-21 Dr. M.E. Fayad Observer Pattern: Applicability An object should be separated into two basic parts: –One part encapsulates state and operations that define the object independent of its context. –The other part encapsulates the mechanisms that present information in context-dependent way. A dependency between an object (Subject) and a set of other objects (Observers) is required such that the observers are notified when the subject undergoes a significant change.

22 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-22 PS95&96-MEF-L16-22 Dr. M.E. Fayad Observer Pattern: Participants Subject –provides an interface for attaching and detaching Observer objects –knows its Observers. Observer –defines an updating interface for objects that should be notified of changes in a Subject. ConcreteSubject –stores state of interest to ConcreteObservers –sends a notification to its Observers when its state changes ConcreteObserver –maintains a reference to its ConcreteSubject –stores state that should stay consistent with the Subject’s. –implements the Observer updating interface to keep its state consistent with the Subject’s.

23 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-23 PS95&96-MEF-L16-23 Dr. M.E. Fayad Observer Pattern: Collaborations Instance Diagram aConcreteSubject aConcreteObserver1 aConcreteObserver2 4. UpdateSelf 6. UpdateSelf 5. Update 1. SetState 3. Update 2. Notify

24 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-24 PS95&96-MEF-L16-24 Dr. M.E. Fayad Observer Pattern: Implementation Issues Issues related to the implementation of the dependency mechanisms: 1. Mapping Subjects to their Observers 2. Avoiding Dangling References to Deleted Subjects 3. Ensuring Self-Consistency of Subject State 4. Avoiding Observer-Specific Update Protocols: The Push and Pull Models 5. Explicitly specifying Modifications of Interest 6. Encapsulating Complex Update Semantics

25 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-25 PS95&96-MEF-L16-25 Dr. M.E. Fayad Discussion Questions o (T) for true or (F) for false ( ) 1. The Mediator pattern replaces many-to-many interactions with one-to-many ones between mediator and colleagues, which are easier to understand, maintain, and extend. ( ) 2. A common use of the Mediator pattern is to keep track of instances of a certain class. ( ) 3. One of the application of the Mediator pattern is to coordinate complex updates. ( ) 4. The Mediator pattern coordinate communication between interacting objects. ( ) 5. The Observer pattern objectifies the notions of context- dependent and context-independent state, permitting them to be varied independently. ( ) 6. The key objects in the Observer pattern are subject and observers. ( ) 7. An Observer pattern should be used when an object has to notify other objects without making any assumptions about who these objects are.


Download ppt "Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-1 PS95&96-MEF-L16-1 Dr. M.E. Fayad Creationa l."

Similar presentations


Ads by Google