Tải bản đầy đủ (.pdf) (19 trang)

LectureConclusionpdf

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

Conclusions



Conclusions



<b>CSIE Department, NTUT</b>


<b>Woei-Kae Chen</b>



2


Discussion of Behavior Patterns


Discussion of Behavior Patterns



z

Encapsulating Variation (with inheritance; p345)



encapsulate an aspect that

changes frequently


znew object: encapsulates the aspect


zexisting object: use the new object


Example



zStrategy Ỉalgorithm


zState Ỉstate-dependent behavior
zIterator Ỉtraversal


zAbstract Factory, Builder Ỉobject creation
zCommand Ỉcommand


zVisitor Ỉoperation



Advantage



znew objects can be changed at run-time
zvary new and existing objects independently
zeasy to add new objects


zeasy to maintain and reuse


Disadvantage



zhave to open interfaces for new objects to access existing objects


</div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=2>

3


Discussion of Behavior Patterns


Discussion of Behavior Patterns



z

Encapsulating Variation (without inheritance)



define an

object

to encapsulate an aspect that

changes


frequently



znew object: encapsulates the aspect
zexisting object: use the new object


Example



zMediator encapsulates the protocol between objects
zPrototype encapsulates object creation



zDecorator encapsulates responsibility added to an object
zBridge encapsulates an abstraction from its implementation


Discussion of Behavior Patterns


Discussion of Behavior Patterns



z

Object as arguments



Visitor



zis the argument to the

<b>accept</b>

operation



Command



zact as tokens to be passed around and invoked later


zthe token represents a request



Memento



</div>
<span class='text_page_counter'>(3)</span><div class='page_container' data-page=3>

5


Discussion of Behavior Patterns


Discussion of Behavior Patterns



z

Should communication be encapsulated or distributed?



Mediator Ù

Observer


zcompeting patterns



Mediator



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


Observer



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


Discussion of Behavior Patterns


Discussion of Behavior Patterns



z

Decoupling Senders and Receivers (1)



Related patterns


zCommand
zObserver

zMediator


zChain of Responsibility


Command



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>


</div>
<span class='text_page_counter'>(4)</span><div class='page_container' data-page=4>

7


Discussion of Behavior Patterns


Discussion of Behavior Patterns



z

Decoupling Senders and Receivers (2)



Observer



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>aSubject</b>


<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>


Discussion of Behavior Patterns


Discussion of Behavior Patterns



z

Decoupling Senders and Receivers (3)



Mediator



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>


</div>
<span class='text_page_counter'>(5)</span><div class='page_container' data-page=5>

9


Discussion of Behavior Patterns


Discussion of Behavior Patterns




z

Decoupling Senders and Receivers (4)



Chain of Responsibility



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


Principle of OO Design


Principle of OO Design



z

Principle of OO Design (p17-20)




<i>Program to an interface, not an implementation</i>



zcommit only to an abstract class interface


zhow to instantiate concrete classes?



–Abstract Factory, Builder, Factory Method, Prototype, and
Singleton.


<i>Favor object composition over class inheritance</i>



zsubclassing: white-box reuse


–inheritance breaks encapsulation


–change parent classes Ỉchange subclasses


–unmanageable monster classes and class hierarchies

zobject composition: black-box reuse



–requires carefully designed interface
–keep each class encapsulated


</div>
<span class='text_page_counter'>(6)</span><div class='page_container' data-page=6>

11


Pattern Structures


Pattern Structures



Design Patterns


Design Patterns



z

Creational patterns




Abstract Factory

,

Builder

,

Factory Method

,


Prototype

,

Singleton



z

Structural patterns (composition)



Adaptor

,

Bridge

,

Composite

,

Decorator

,

Facade

,



Flyweight

,

Proxy



z

Behavioral patterns (interaction)



</div>
<span class='text_page_counter'>(7)</span><div class='page_container' data-page=7>

13


Creational patterns


Creational patterns



14


Abstract Factory: Structure


Abstract Factory: Structure



<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>CreateProductB()</i>


<i><b>AbstractFactory</b></i>


CreateProductA()
CreateProductB()


<b>ConcreteFactory1</b>


CreateProductA()
CreateProductB()


<b>ConcreteFactory2</b>


</div>
<span class='text_page_counter'>(8)</span><div class='page_container' data-page=8>

15


Builder pattern: Structure


Builder pattern: Structure



+Construct()
<b>Director</b>


for all objects in structure {
builder->BuildPart()
}


+BuildPart()
<b>Builder</b>
builder



+BuildPart()
+GetResult()


<b>ConcreteBuilder</b>


<b>Product</b>

<b>Client</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;


Factory Method: Structure


Factory Method: Structure




</div>
<span class='text_page_counter'>(9)</span><div class='page_container' data-page=9>

17


Prototype: Structure


Prototype: Structure



<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


<b>Singleton</b>


static Instance()


SingletonOperation()


GetSingletonData()



static uniqueInstance


singletonData



Singleton pattern: Structure


Singleton pattern: Structure



</div>
<span class='text_page_counter'>(10)</span><div class='page_container' data-page=10>

19


Structural patterns


Structural patterns



Adapter: Structure (1)


Adapter: Structure (1)


Class adapter: use multiple inheritance



<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>


</div>
<span class='text_page_counter'>(11)</span><div class='page_container' data-page=11>

21


Bridge pattern: Structure



Bridge pattern: Structure



+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


Adapter: Structure (2)


Adapter: Structure (2)


Object adapter: use object composition



<i><b>Target</b></i>
<i><b>Request()</b></i>


<b>Adapter</b>


<b>Request()</b>


<b>Adaptee</b>


<b>SpecificRequest()</b>
<b>Client</b>


<b>adaptee->SpecificRequest()</b>


</div>
<span class='text_page_counter'>(12)</span><div class='page_container' data-page=12>

23


Composite: Structure


Composite: Structure



<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>


<i><b>Add(Component)</b></i>
<i><b>Remove(Component)</b></i>
<i><b>GetChild(int)</b></i>


Decorator: Structure


Decorator: Structure



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>


</div>
<span class='text_page_counter'>(13)</span><div class='page_container' data-page=13>

25


Facade: Structure


Facade: Structure



<b>Facade</b>


<b>subsystem classes</b>


<b>Client</b>


26


Flyweight: Structure


Flyweight: Structure



</div>
<span class='text_page_counter'>(14)</span><div class='page_container' data-page=14>

27


Proxy: Structure


Proxy: Structure



<i>Request()</i>
...()


<i><b>Subject</b></i>


Request()
...()



<b>Proxy</b> ...


realSubject->Request();
...


<b>Client</b>


realSubject
Request()


...()


<b>RealSubject</b>


</div>
<span class='text_page_counter'>(15)</span><div class='page_container' data-page=15>

29


Command: Structure


Command: Structure



<i><b>Command</b></i>
<i>Execute()</i>


receiver->Action()
receiver


<b>ConcreteCommand</b>
Execute()


state


Invoker


Client


<b>Receiver</b>
Action()


30


Mediator: Structure


Mediator: Structure



<b>mediator</b>


<i><b>Mediator</b></i>


<b>ConcreteMediator</b>


<i><b>Colleague</b></i>


</div>
<span class='text_page_counter'>(16)</span><div class='page_container' data-page=16>

31


Observer: Structure


Observer: Structure


<b>Subject</b>


<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>


State: Structure


State: Structure



<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>


</div>
<span class='text_page_counter'>(17)</span><div class='page_container' data-page=17>

33


Strategy: Structure


Strategy: Structure



<b>Context</b>



<b>+ContextInterface()</b>


<i><b>Strategy</b></i>


<i><b>+Algorithminterface()</b></i>


<b>ConcreteStrategyA</b>


<b>+Algorithminterface()</b>


<b>ConcreteStrategyB</b>


<b>+Algorithminterface()</b>



<b>ConcreteStrategyC</b>


<b>+Algorithminterface()</b>


34


Template Method: Structure


Template Method: Structure



<i>Abstract Class</i>


TemplateMethod()


<i>PrimitiveOperation1()</i>
<i>PrimitiveOperation2()</i>




PrimitiveOperation1()


PrimitiveOperation2()


</div>
<span class='text_page_counter'>(18)</span><div class='page_container' data-page=18>

35


Visitor: Structure


Visitor: Structure



<b>ObjectStructure</b>


<b>Client</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>



Design patterns: Conclusion


Design patterns: Conclusion



z

Documents existing designs



used by expert object-oriented designers



z

What to expect from design patterns (p351)



A Common Design Vocabulary



zto communicate, document, and explore design alternatives
zmake a system seem less complex Åtalking about it in a


higher level


A Documentation and Learning Aid



zmost large OO system use these design patterns


</div>
<span class='text_page_counter'>(19)</span><div class='page_container' data-page=19>

37


Design patterns: Conclusion


Design patterns: Conclusion



An Adjunct to Existing Methods



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


A Target for Refactoring



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


Design patterns: Conclusion


Design patterns: Conclusion


z

Expansionary phase



– add new classes and operations
– new requirements are satisfied, but ...



zbecomes too inflexible for further change


zclasses define many unrelated operations and instance variables


z

Consolidating phase



– 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

z

Design patterns



– capture many structures results from refactoring
– provide targets for refactorings.


<b>expansion</b>


</div>

<!--links-->

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×