Presentation is loading. Please wait.

Presentation is loading. Please wait.

June 26, 20151 Design Patterns  What is a Design Pattern -- Each pattern describes a problem which occurs over and over again in our environment, and.

Similar presentations


Presentation on theme: "June 26, 20151 Design Patterns  What is a Design Pattern -- Each pattern describes a problem which occurs over and over again in our environment, and."— Presentation transcript:

1 June 26, 20151 Design Patterns  What is a Design Pattern -- Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice(by Christopher Alexander)  Essential elements of a Design Pattern - pattern name - problem - solution - consequence

2 June 26, 20152 Classification of design pattern  According to its purpose -- Creational Factory Method, Abstract Factory, Builder, Prototype,Singleton -- Structural Adapter, Bridge, Composite, Decorator, Façade, Flyweight, Proxy -- Behavioral Interpreter, Template Method, Chain of Responsibility, command, Iterator, Mediator, Memento, Observer, State, Strategy, Visitor

3 June 26, 20153 Classification of design pattern(continued)  According to its scope class -- class Factory Method, Adapter(class), Interpreter, Template Method Object -- Object The rest of the patterns

4 June 26, 20154 Patterns with possible use in framework  Creational Patterns including Abstract Factory, Builder, Factory Method and Prototype  Behavioral Pattern Command, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor Notes: Factory Method and Template Method are class patterns

5 June 26, 20155 Abstract Factory Object Creational Abstract Factory Object Creational  Intent Provide an interface for creating families of related or dependent objects without specifying their concrete class  Motivation Consider a user interface toolkit that supports multiple look-and- feel standards, such as Motif and Presentation Manager. Different look- and-feels define different appearances and behaviors for user interface “widgets” like scroll bars, windows, and buttons. To be portable across look-and-feel standards, an application should not hard-code its widgets for a particular look-and-feel. Instantiating look- and-feel specific classes of widgets throughout the application makes it hard to change the look-and- feel later

6 June 26, 20156 Window Client MOtifWindowPMWindow ScrollBar MotifScorllBarPMScrollBar WidgetFactory createScrollBar() CreateWindow() MotifWidgetFactory PMWidgetFactory createScrollBar() CreateWindow() createScrollBar() CreateWindow()

7 June 26, 20157  Structure

8 June 26, 20158  Collaborations -- Normally a single instance of a ConcreteFactory class is created at run-time. This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory -- Normally a single instance of a ConcreteFactory class is created at run-time. This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory -- AbstractFactory defers creation of product objects to its -- AbstractFactory defers creation of product objects to its ConcreteFactory subclass ConcreteFactory subclass

9 June 26, 20159 Builder -- Object Creational  Intent -- Separate the construction of a complex object from its representation so that the same construction process can create different representation  Motivation -- A reader for the RTF(Rich Text Format) document exchange format should be able to convert RTF to many text formats. The reader might convert RTF documents into plain ASCII text or into a text widget that can be edited interactively. The problem, however, is that the number of possible conversions is open-ended. So it should be easy to add a new conversion without modifying the reader.

10 June 26, 201510 ConvertCccharacter(char) GetASCIITEXT() ASCIIConverter TeXConverter ConvertCccharacter(char) convertFontChange(Font) convertParagraph() GetTeXtext() TeXText ASCIIText TexWidget While(t=get the next token{ Switch t.Type{ CHAR: Builder->ConvertCharacter(t.Char) FONT: Builder->ConvertFontChange(t.Font) PARA: Biolder->convertParagraph() } TexWidgetConverter ConvertCccharacter(char) convertFontChange(Font) convertParagraph() GetTextWidget() ConvertCccharacter(char) convertFontChange(Font) convertParagraph() TextConverter builders RTFReader ParseRTF() builder

11 June 26, 201511  Structure

12 June 26, 201512  Collaborations -- The client creates the Director object and configures it with the desired Builder object -- Director notifies the builder whenever a part of the product should be built -- The client retrieves the product from the builder

13 June 26, 201513 Factory Method Class Creational Factory Method Class Creational  Intent Define an interface for creating an object, but let subclass decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses  Motivation Frameworks use abstract classes to define and maintain relationships between objects. A framework is often responsible for creating these objects as well. Consider a framework for applications that can present multiple documents to the user. Tow key abstractions in this framework are the classes Application and Document. Both classes are abstract,and clients have to subclass them to realize their application-specific implementations. To create a drawing application,for example,we define the class DrawingApplciation and DrawingDocument. The Application class is responsible for managing Documents and will create them as required-when when the user selects Open or New from a menu, for example.

14 June 26, 201514 MyDocument Document Open() Close(0 Save() Revert() createDocument() NewDocument() openDocument() Application createDocument() MyApplication Document* doc= createDocument(); Docs.Add(doc); Doc->open() Return new MyDocument docs

15 June 26, 201515  Structure

16 June 26, 201516  Collaborations -- Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct

17 June 26, 201517 Prototype Object Creational Prototype Object Creational  Intent Specify the kinds of Objects to create using a prototypical instance,and create new objects by copying this prototype  Motivation You could build an editor for music scores by customizing a general framework for graphical editors and adding new objects that represent notes,rests, and staves. The editor framework may have a palette of tools for adding these music objects to the score. The palette would also include tools for selecting, moving, and otherwise manipulation music objects. Users will click on the quarter-note tool and use it to add quarter notes to the score. Or they can use the move tool to move a note up or down on the staff, thereby changing its pitch

18 June 26, 201518 Let’s assume the framework provides an abstract Graphics class for graphical components, like notes and staves. Moreover, it’ll provide an abstract Tool class for defining tools like those in the palette. The framework also predefines a GraphicTool subclass for tools that create instances of graphical objects and add them to the document

19 June 26, 201519  structure

20 June 26, 201520  Collaborations A client asks a prototype to clone itself

21 June 26, 201521 Command Object Behavioral Command Object Behavioral  Intent Encapsulate a request as an object,thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations  Motivation Sometimes it’s necessary to issue Sometimes it’s necessary to issue

22 June 26, 201522  Structure

23 June 26, 201523  Collaborations -- The clients creates a ConcreteCommand ojbect and specifies its reciever

24 June 26, 201524 Iterator Object Creational Iterator Object Creational  Intent Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation  Motivation List Count() Append(Element) Remove(Element) … ListIterator First() Next() IsDone() CurrentItem() index list

25 June 26, 201525  Structure

26 June 26, 201526  Collaborations -- A ConcreteIterator keeps track of the current object in the aggregate and can compute the succeeding object in the traversal

27 June 26, 201527 Mediator Mediator Object Behavioral  Intent Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.  What is a Mediator -- A mediator is responsible for controlling and coordinating the interactions of a group of objects. The mediator serves as an intermediary that keeps objects in the group form referring to each other explicitly. The objects only know the mediator, thereby reducing the number of interconnections

28 June 26, 201528  Example aClient director aListBox director aFontDialogDirector aButton director anEntryField director

29 June 26, 201529  Structure

30 June 26, 201530  Collaborations Colleagues send and receive requests from a Mediator object. The mediator implements the cooperative behavior by routing requests between the appropriate colleague(s)

31 June 26, 201531 Observer Object Behavioral Observer Object Behavioral  Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically  Example A = 50% B= 30% C= 20% observers subject Change notification Requests, modifications Requests, modifications x y z a b c 60 50 80 30 10 20 10

32 June 26, 201532  Structure

33 June 26, 201533  Collaboration - ConcreteSubject notifies its observers whenever a change occurs that could make its observers’ state inconsistent with its own. - After being informed of a change in the concrete subject, a ConcreteObserver object may query the subject for information. ConcreteObserver uses this information to reconcile its state with that of the subject.

34 June 26, 201534 State Object Behavioral State Object Behavioral  Intent Allow an object to alter its behavior when its internal state changes. The Object will appear to change its class  Example State->Open TCPConnection Open() Close() Acknowledge() TcpState Open() Close() Acknowledge() TCPEstablishedTCPListenTCPClosed Open() Close() Acknowledge() Open() Close() Acknowledge() Open() Close() Acknowledge()

35 June 26, 201535  Structure

36 June 26, 201536  Collabortions - Context delegates state-specific requests to the current ConcreteState object. - Context is the primary interface for clients. Clients can configure a context with State objects. Once a context is configured, its clients don’t have to deal with the State objects directly. - Either Context or the ConcreteState subclasses can decide which state succeeds another and under what circumstances

37 June 26, 201537  Intent Define a family of algorithms, encapsulate each one, and make them interchangeable.Strategy lets the algorithm vary independently from clients that use it  Example Compositor->compose() Composition Traverse() Repair() Compositor compose() Compose() compose() SimpleComposerArrayCompositor Strategy object Behavioral Strategy object Behavioral TexCompositor compose()

38 June 26, 201538  Structure

39 June 26, 201539  Collaborations - Strategy and Context interact to implement the chosen algorithm. A context may pass all data required by the algorithm to the strategy when the algorithm is called. Alternatively, the context can pass itself as an argument to Strategy operations. That lets the strategy call back on the context as required. - A context forwards requests from its clients to its strategy. Clients usually create and pass a ConcreteStrategy object to the context; thereafter, clients interact with the context exclusively. There is often a family of ConcreteStrategy classes for a client to choose from

40 June 26, 201540 Template Method Class Behavioral Template Method Class Behavioral  Intent Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.  Example Application AddDocument() openDocument() DoCreateDocument() canOpenDocument() AboutToOpenDocument MyApplication DoCreateDocument() canOpenDocument() AboutToOpenDocument Document Save() Open() Close() doRead() MyDocument DoRead() Return new MyDocument docs

41 June 26, 201541  Structure

42 June 26, 201542  Collaboration ConcreteClass relies on AbstractClass to implement the invariant steps of the algorithm

43 June 26, 201543 Visitor Object Behavioral Visitor Object Behavioral  Intent Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates  Example VariableRefNode AssignmentNode TypeCheck() GenerateCode() PrettyPrint() TypeCheck() GenerateCode() PrettyPrint() TypeCheck() GenerateCode() PrettyPrint() Node

44 June 26, 201544  Structure

45 June 26, 201545  Collaboration - A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor - A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor - When an element is visited, it calls the visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary. - When an element is visited, it calls the visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary.


Download ppt "June 26, 20151 Design Patterns  What is a Design Pattern -- Each pattern describes a problem which occurs over and over again in our environment, and."

Similar presentations


Ads by Google