250 likes | 393 Views
Behavioral. Paradigm Shift, Inc. Software Factory. Creational. Structural. Lesson 9: More On Behavioral Patterns. Object-Oriented. Patterns. Design. Lesson Objectives. Present the following Patterns: Mediator Observer Discuss in detail the behavioral patterns. Behavioral Patterns.
E N D
Behavioral Paradigm Shift, Inc. Software Factory Creational Structural Lesson 9:More On Behavioral Patterns Object-Oriented Patterns Design
Lesson Objectives • Present the following Patterns: • Mediator • Observer • Discuss in detail the behavioral patterns
Behavioral Patterns • Topics • Mediator Definition • Structure • Graphic UI Example • Problems it Solves • Participants • Benefits • Related Patterns • Mediator Pattern
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.
Mediator Pattern: Illustration Client or User Object A The “Before” Model Object B Object C Object D
Mediator Pattern: More Illustration Client or User Object A Object B Colleague objects Mediator Object Object C Object D The “After” Model Abstraction
Mediator colleague classes Mediator Pattern: Structure
Dialog Director Example director DialogDirector Control director-> HandleChange(this) ShowDialog() CreateControls() HandleChange(Control) Select() EntryField ListBox ListDialog CreateControls() HandleChange(Control) • 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.
Dialog Director Example: Interaction Diagram Mediator Colleague EntryField Client ListBox DialogDirector HandleChange GetSelection ShowDialog SetText
Mediator Pattern: Participants & Collaborations Participants • 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).
Mediator Pattern: Restaurant Example • 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 Customer in a restaurant Eggs Fry Toast Fry2 The “Before” Model
Mediator Pattern: The Solution Customer in a Restaurant Eggs Fry Colleague objects Waitperson Fry2 Toast The “After” Model Abstraction
Mediator Pattern: More on The Solution • 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: 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.
Mediator Pattern: Disadvantages • 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: When It Should be Used • 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.
Behavioral Patterns • Topics • Observer Definition • Structure • Application Area • Problems it Solves • Participants • Benefits • Implementation Issues • Discussion Questions • Observer Pattern
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.
Observer Pattern: Structure observers Subject Observer Attach(Observer) Detach(Observer) Notify() Update() • for all o in observers { • o->Update() • } ConcreteObserver ConcreteSubject subject Update() GetState() observerState subjectState observerState = subject->GetState(); return subjectState
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.
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.
Observer Pattern: CollaborationsInstance Diagram 4. UpdateSelf 6. UpdateSelf aConcreteObserver1 aConcreteObserver2 1. SetState 3. Update 5. Update aConcreteSubject 2. Notify
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
Discussion Questions • (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.