copyright © 1995-2004 active frameworks inc. - all rights reserved.more on behavioral patterns -...

25
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 Creation al Paradigm Shift, Inc. Software Factory Behavior al Structur al Lesson 9: More On Behavioral Patterns Object- Object- Oriented Oriented Design Design Pattern Pattern s s

Post on 22-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-1

PS95&96-MEF-L16-1Dr. M.E. Fayad

Creational

Paradigm Shift, Inc.Software Factory

Behavioral

Structural

Lesson 9:More On Behavioral Patterns

Lesson 9:More On Behavioral Patterns

Object-Object-OrientedOriented

DesignDesign PatternPatternss

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-2

PS95&96-MEF-L16-2Dr. M.E. Fayad

Lesson ObjectivesLesson Objectives

Objectives

Present the following Patterns:

Mediator

Observer

Discuss in detail the behavioral patterns

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-3

PS95&96-MEF-L16-3Dr. M.E. Fayad

Behavioral Patterns

Mediator Pattern

• Topics– Mediator Definition

– Structure

– Graphic UI Example

– Problems it Solves

– Participants

– Benefits

– Related Patterns

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-4

PS95&96-MEF-L16-4Dr. 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.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-5

PS95&96-MEF-L16-5Dr. M.E. Fayad

Mediator Pattern: Illustration

Client or User Object A

Object C

Object D

Object BThe “Before” Model

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-6

PS95&96-MEF-L16-6Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-7

PS95&96-MEF-L16-7Dr. M.E. Fayad

Mediator Pattern: Structure

Mediator

colleague classes

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-8

PS95&96-MEF-L16-8Dr. 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.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-9

PS95&96-MEF-L16-9Dr. M.E. Fayad

Dialog Director Example: Interaction Diagram

GetSelection

DialogDirector

Colleague

Client

SetText

HandleChange

ListBox

ShowDialog

Mediator

EntryField

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-10

PS95&96-MEF-L16-10Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-11

PS95&96-MEF-L16-11Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-12

PS95&96-MEF-L16-12Dr. M.E. Fayad

Mediator Pattern: Restaurant Example

Customer in a restaurant Eggs

Toast

Fry2

Fry

The “Before” Model

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-13

PS95&96-MEF-L16-13Dr. M.E. Fayad

Mediator Pattern: The Solution

Customer in a Restaurant Eggs

Fry2

Toast

Fry

The “After” Model

Waitperson

Abstraction

Colleague objects

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-14

PS95&96-MEF-L16-14Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-15

PS95&96-MEF-L16-15Dr. 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.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-16

PS95&96-MEF-L16-16Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-17

PS95&96-MEF-L16-17Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-18

PS95&96-MEF-L16-18Dr. M.E. Fayad

Behavioral Patterns

Observer Pattern

• Topics– Observer Definition

– Structure

– Application Area

– Problems it Solves

– Participants

– Benefits

– Implementation Issues

– Discussion Questions

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-19

PS95&96-MEF-L16-19Dr. 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.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-20

PS95&96-MEF-L16-20Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-21

PS95&96-MEF-L16-21Dr. 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.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-22

PS95&96-MEF-L16-22Dr. 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.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-23

PS95&96-MEF-L16-23Dr. M.E. Fayad

Observer Pattern: CollaborationsInstance Diagram

aConcreteSubject

aConcreteObserver1 aConcreteObserver2

4. UpdateSelf 6. UpdateSelf

5. Update

1. SetState3. Update

2. Notify

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-24

PS95&96-MEF-L16-24Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved. More On Behavioral Patterns - Page L9-25

PS95&96-MEF-L16-25Dr. M.E. Fayad

Discussion QuestionsDiscussion 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.