Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (433.36 KB, 19 trang )
<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1>
1
2
zexisting object: use the new object
zStrategy Ỉalgorithm
zState Ỉstate-dependent behavior
zIterator Ỉtraversal
zAbstract Factory, Builder Ỉobject creation
zCommand Ỉcommand
zVisitor Ỉoperation
znew objects can be changed at run-time
zvary new and existing objects independently
zeasy to add new objects
zeasy to maintain and reuse
zhave to open interfaces for new objects to access existing objects
3
znew object: encapsulates the aspect
zexisting object: use the new object
zMediator encapsulates the protocol between objects
zPrototype encapsulates object creation
zDecorator encapsulates responsibility added to an object
zBridge encapsulates an abstraction from its implementation
5
zencapsulates communication between objects
zcentralizes communications
zmaintains a communication constraint in the mediator
zdifficult to make reusable Mediators
zeasier to understand the flow of communication
zdistributes communication by introducing Observer and Subject
objects
zobserver and subject cooperate to maintain a constraint
zeasier to make reusable observers and subjects
zdifficult to understand the flow of communication
6
zChain of Responsibility
zdecouples invokers and receiversby command objects
zallow senders to work with different receivers
za subclass for a sender-receiver connection
<b>anInvoker</b>
<b>(sender)</b> <b>aCommand</b> <b>aReceiver(receiver)</b>
<b>Execute()</b>
7
zdecouples subjects and observersby an interface for
signaling changes
za subject may have multiple observers
zthe number of observers can vary at run-time
zbest for decoupling objects with data dependencies
<b>(sender)</b> <b>anObserver(receiver)</b> <b>anObserver(receiver)</b>
<b>Update()</b>
<b>anObserver</b>
<b>(receiver)</b>
<b>Update()</b>
<b>Update()</b>
zdecouple objects (Colleagues)through a Mediator object
zroutes requests between Colleague objects
zcentralizes communication between Colleague objects
<b>aColleague</b>
<b>(sender/receiver)</b> <b>aMediator</b> <b>(sender/receiver)aColleague</b>
9
zdecouple client and handlerby passing the request along a chain
zwhen the chain is part of the system’s structure
zwhen one of several objects may be in a position to handle the
request
<b>aClient</b>
<b>(sender)</b> <b>(receiver)aHandler</b> <b>aHandler(receiver)</b>
<b>HandleHelp()</b>
<b>aHandler</b>
<b>(receiver)</b>
<b>HandleHelp()</b>
<b>HandleHelp()</b>
10
–Abstract Factory, Builder, Factory Method, Prototype, and
Singleton.
–change parent classes Ỉchange subclasses
–unmanageable monster classes and class hierarchies
–requires carefully designed interface
–keep each class encapsulated
11
13
14
<i><b>AbstractProductA</b></i>
<b>ProductA2</b> <b>ProductA1</b>
<i><b>AbstractProductB</b></i>
<b>ProductB2</b> <b>ProductB1</b>
<i>CreateProductA()</i>
<i><b>AbstractFactory</b></i>
CreateProductA()
CreateProductB()
<b>ConcreteFactory1</b>
CreateProductA()
CreateProductB()
<b>ConcreteFactory2</b>
15
+Construct()
<b>Director</b>
for all objects in structure {
builder->BuildPart()
}
+BuildPart()
<b>Builder</b>
builder
+BuildPart()
+GetResult()
<b>ConcreteBuilder</b>
<b>Product</b>
<i><b>Product</b></i>
<b>ConcreteProduct</b>
<i>FactoryMethod()</i>
AnOperation()
<i><b>Creator</b></i>
FactoryMethod()
<b>ConcreteCreator</b>
...
product = FactoryMethod();
...
return new ConcreteProduct;
17
<b>ConcretePrototype2</b>
<b>Client</b>
Operation()
<b>Prototype</b>
Clone()
<b>ConcretePrototype1</b>
Clone() Clone()
prototype
p=prototype->Clone()
return copy of self return copy of self
18
19
<i><b>Target</b></i>
<i><b>Request()</b></i>
<b>Adapter</b>
<b>Request()</b>
<b>Adaptee</b>
<b>SpecificRequest()</b>
<b>SpecificRequest()</b>
21
+Operation()
<b>Abstraction</b>
<b>RefinedAbstraction</b>
imp->OperationImp()
+OperationImp()
<b>Implementor</b>
+OperationImp()
<b>ConcreteImplementorA</b>
imp
+OperationImp()
<b>ConcreteImplementorB</b>
<b>Client</b>
22
<i><b>Target</b></i>
<i><b>Request()</b></i>
<b>Adapter</b>
<b>Adaptee</b>
<b>SpecificRequest()</b>
<b>Client</b>
<b>adaptee->SpecificRequest()</b>
23
<b>Composite</b>
<b>Operation()</b>
<b>Add(Component)</b>
<b>Remove(Component)</b>
<b>GetChild(int)</b>
<b>Client</b>
<b>for all g in children</b>
<b>g.Operation() ;</b>
<b>Leaf</b>
<b>Operation()</b>
<i><b>Component</b></i>
<i><b>Operation()</b></i>
Operation()
<i><b>Decorator</b></i>
Operation()
AddedBehavior()
<b>ConcreteDecoratorB</b>
component->Operation();
Decorator::Operation();
AddedBehavior();
Operation()
<b>ConcreteComponent</b>
Operation()
addedState
<b>ConcreteDecoratorA</b>
<i>Operation()</i>
<i><b>Component</b></i>
25
<b>Facade</b>
<b>subsystem classes</b>
<b>Client</b>
26
27
<i>Request()</i>
...()
<i><b>Subject</b></i>
Request()
...()
<b>Proxy</b> ...
realSubject->Request();
...
<b>Client</b>
realSubject
Request()
...()
<b>RealSubject</b>
29
<i><b>Command</b></i>
<i>Execute()</i>
receiver->Action()
receiver
<b>ConcreteCommand</b>
Execute()
state
Client
<b>Receiver</b>
Action()
30
<b>mediator</b>
<i><b>Mediator</b></i>
<b>ConcreteMediator</b>
<i><b>Colleague</b></i>
31
<b>Attach(Observer)</b>
<b>Detach(Observer)</b>
<b>Notify()</b>
<b>ConcreteSubject</b>
<b>GetState()</b>
<b>SetState()</b>
<b>subjectState</b>
<b>Observer</b>
<b>Update()</b>
<b>ConcreteObserver</b>
<b>Update()</b>
<b>observerState</b>
<b>observerState = </b>
<b>subject->GetState()</b>
<b>return subjectState</b>
<b>observers</b>
<b>subject</b>
<b>for all o in observers</b>
<b>o->Update();</b>
<i><b>State</b></i>
<i><b>Handle()</b></i>
<b>ConcreteStateA</b>
<b>Handle()</b>
<b>ConcreteStateB</b>
<b>Handle()</b>
<b>state->Handle()</b>
<b>Context</b>
<b>Request()</b>
33
<b>+ContextInterface()</b>
<b>ConcreteStrategyA</b>
<b>+Algorithminterface()</b>
<b>ConcreteStrategyB</b>
<b>+Algorithminterface()</b>
<b>ConcreteStrategyC</b>
<b>+Algorithminterface()</b>
34
<i>Abstract Class</i>
TemplateMethod()
<i>PrimitiveOperation1()</i>
<i>PrimitiveOperation2()</i>
…
PrimitiveOperation1()
…
PrimitiveOperation2()
…
35
<b>ObjectStructure</b>
<b>v->VisitConcreteElementA(this)</b>
<i><b>Visitor</b></i>
<i><b>VisitConcreteElementA(ConcreteElementA)</b></i>
<i><b>VisitConcreteElementB(ConcreteElementB)</b></i>
<b>ConcreteVisitor2</b>
<b>VisitConcreteElementA(ConcreteElementA)</b>
<b>VisitConcreteElementB(ConcreteElementB)</b>
<b>ConcreteVisitor1</b>
<b>VisitConcreteElementA(ConcreteElementA)</b>
<b>VisitConcreteElementB(ConcreteElementB)</b>
<i><b>Element</b></i>
<i><b>Accept(Visitor)</b></i>
<b>ConcreteElementA</b>
<b>Accept(Visitor v)</b>
<b>OperationA()</b>
<b>ConcreteElementB</b>
<b>Accept(Visitor v)</b>
<b>OperationB()</b>
<b>v->VisitConcreteElementB(this)</b>
zto communicate, document, and explore design alternatives
zmake a system seem less complex Åtalking about it in a
higher level
zmost large OO system use these design patterns
37
zis an important missing piece from OO design methods
zshow how to use primitive techniques (e.g., objects,
inheritance, and polymorphism)
zprovide a way to describe more of the “why” Ỉnot just
record the results of decisions.
zuseful in turning an analysis model into an implementation
model
zOO software lifecycle
–prototyping phase
–expansionary phase
–consolidating phase
<b>expansion</b>
<b>prototyping</b> <b>consolidation</b>
<b>more requirements more reuse</b>
38
– add new classes and operations
– new requirements are satisfied, but ...
zbecomes too inflexible for further change
zclasses define many unrelated operations and instance variables
– moving operations up or down the class hierarchy
– rationalizing the interface of classes
– tearing apart classes into special- and general-purpose components
zproduce many new kinds of objects
zdecomposing existing objects using object composition instead of
inheritance
zblack box reuse replace white box reuse.
– software becomes more general
– capture many structures results from refactoring
– provide targets for refactorings.
<b>expansion</b>