2007 pearson education, inc. publishing as pearson addison-wesley 1 broker design patterns: faade...
DESCRIPTION
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3 Topics Broker patterns The Façade pattern The Mediator patternTRANSCRIPT
1© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Broker Design Broker Design Patterns: Façade and Patterns: Façade and MediatorMediator
2© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
ObjectivesObjectives
To present the structure, behavior, and characteristics of broker patterns
To present the Façade and Mediator design patterns
3© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
TopicsTopics
Broker patterns The Façade pattern The Mediator pattern
4© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Broker Pattern Broker Pattern StructureStructure
SupplierBrokerClient
The Client must access the Broker and the Broker must access the Supplier
Most Broker patterns elaborate this basic structure
5© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Broker Pattern Broker Pattern BehaviorBehavior
:Broker:Client :Supplier
requestService()obtainService()
sd BrokerBehavior
6© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Broker Pattern Broker Pattern AdvantagesAdvantages
Simplify the Supplier—A Broker can augment the Supplier’s services.
Decompose the Supplier—A complex Supplier can offload some of its responsibilities to a Broker.
Facilitate Client/Supplier Interaction—A Broker may present a different interface, handle interaction details, etc.
7© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Broker Example: Broker Example: Iterator FormIterator Form
«supplier»ConcreteCollection
«broker»ConcreteIterator
* 1
iterator() : Iterator
«interface»Collection
«interface»Iterator
Client reset()isDone() : booleangetCurrent() : Objectnext()
8© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Broker Example: Iterator Broker Example: Iterator BehaviorBehavior
i:ConcreteIterator:Client :ConcreteCollection
reset()
e=getCurrent()
next()
done=isDone()
done=isDone()
checkStatus()
fetchValue()
checkStatus()
loop [!done]
sd IteratorAsBroker
9© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Façade PatternThe Façade Pattern
The Façade pattern eases interaction between a client and a sub-system of suppliers by providing a simpler interface to the sub-system.
The broker class is a façade that provides simplified sub-system services to clients.
Analogy: a travel agent
10© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Façade Pattern Façade Pattern StructureStructure
Façade
Complex Subsystem
Client
11© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Façade ExamplesFaçade Examples
Interface to a compiler Interface to a user interface
(from the application side) Interface to a memory
management system
12© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
When to Use a FaçadeWhen to Use a Façade Use the Façade pattern when there is
a need to provide a simplified interface to a complex sub-system.
Façades can also help decouple systems.• If the façade mediates all interaction with
a client, then the sub-system can be changed without affecting the client.
A façade may work like an adapter by providing a new interface to a sub-system (adapters are discussed later).
13© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Mediator PatternThe Mediator Pattern
The Mediator pattern reduces coupling and simplifies code when several objects must negotiate a complex interaction.
Classes interact only with a mediator class rather than with each other.
Classes are coupled only to the mediator where interaction control code resides.
Mediator is like a multi-way Façade pattern.
Analogy: a meeting scheduler
14© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Using a MediatorUsing a Mediator
collaboratorA
mediator
collaboratorD
collaboratorC
collaboratorB1: op1() 2.1: op2()
1.3: op3()
3: op4()
2: op2()
collaboratorA
collaboratorD
collaboratorC
collaboratorB
1.2: op2()1.5: op2()1.1: op1()
1.4: op4()
1: op()
UnmediatedCollaboration
MediatedCollaboration
2.2: op3()
15© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Mediator Pattern Mediator Pattern StructureStructure
Mediator Collaborator
ColleagueB
ColleagueA
ColleagueC
16© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Mediator as a BrokerMediator as a Broker
«broker»Mediator
Collaborator
«supplier»ColleagueB
«client»ColleagueA
«supplier»ColleagueC
17© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Mediator BehaviorMediator Behavior
self:Mediator :ColleagueA
consult()
:ColleagueB :ColleagueC
consult()
consult()
notify()
sd requestService()
18© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
When to Use a When to Use a MediatorMediator
Use the Mediator pattern when a complex interaction between collaborators must be encapsulated to
• Decouple collaborators,• Centralize control of an interaction, and• Simplify the collaborators.
Using a mediator may compromise performance.
19© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Mediators, Façades, and Mediators, Façades, and Control StylesControl Styles
The Façade and Mediator patterns provide means to make control more centralized.
The Façade and Mediator patterns should be used to move from a dispersed to a delegated style, but not from a delegated to a centralized style.
20© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
SummarySummary Broker patterns use a Broker class to
facilitate the interaction between a Client and a Supplier.
The Façade pattern uses a broker (the façade) to provide a simplified interface to a complex sub-system.
The Mediator pattern uses a broker to encapsulate and control a complex interaction among several suppliers.