45
Design Pattern By Julie Iskander MSc. Communication and Electronics

Design Pattern lecture 4

Embed Size (px)

DESCRIPTION

Behavioral Design Patterns Chain of Responsibility Command Mediator Memento Observer State Strategy Template Method Interpreter Visitor Iterator

Citation preview

Page 1: Design Pattern lecture 4

Design PatternBy Julie Iskander

MSc. Communication and Electronics

Page 2: Design Pattern lecture 4

Outlines Lecture 4

• Behavioral Design Patterns• Chain of Responsibility• Command• Mediator• Memento• Observer• State• Strategy• Template Method• Interpreter• Visitor• Iterator (Reading Assignment)

Page 3: Design Pattern lecture 4

Behavioral Patterns

Page 4: Design Pattern lecture 4

Chain of responsibility DP

• What• A replication of the business or functional process of

delegating responsibility within a hierarchy.

• Where • A requirement to manage tasks by coordinating objects and

have them cooperate within a hierarchical structure.

• Why• Avoid coupling sender of request to receiver

• How• An abstract class that represents a participant or link is

sub-classed into a set of links, and then the sub-classed links implement the functionality that represents their responsibility and can trigger the passing on of a requirement. The client determines the hierarchy among the links and initiates passing the requirement to the chain.

Page 5: Design Pattern lecture 4

Chain of responsibility dp

Page 6: Design Pattern lecture 4

Chain of responsibility dp

Page 7: Design Pattern lecture 4

Chain of responsibility dp

Page 8: Design Pattern lecture 4

Chain of responsibility

Page 9: Design Pattern lecture 4

Chain of responsibility

Page 10: Design Pattern lecture 4

Command DP(object - Behavioral DP)

Page 11: Design Pattern lecture 4

Command DP(object - Behavioral DP)

•What• Encapsulate a request as an object, and

support undoable operations.

•Where • To issue requests to objects without knowing

anything about the operation being requested or the receiver of the request.

•Why• specify, queue, and execute requests at

different times. support undo. support logging changes

Page 12: Design Pattern lecture 4

Command DP(object - Behavioral DP)

Page 13: Design Pattern lecture 4

Command DP(object - Behavioral DP)

Page 14: Design Pattern lecture 4

Command DP(object - Behavioral DP)

Page 15: Design Pattern lecture 4

Command DP(object - Behavioral DP)

Page 16: Design Pattern lecture 4

• Supporting undo and redo:• Must provide a reverse method

(Unexecute/Undo).• Might need to store additional state like the

Receiver object, the arguments to the operation performed on the receiver, and any original values in the receiver that can change as a result of handling the request.

• For one level of undo, store only the last executed command.

• For multi-level undo, store a history list of executed commands.

Command DP(object - Behavioral DP)

Page 17: Design Pattern lecture 4

Mediator DP (object - Behavioral DP)

• What• 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.

• Where • when many objects interconnect, to lessen the coupling

• Why• To lessen the coupling between objects

• How• Encapsulating collective behavior in a separate mediator

object. A mediator is responsible for controlling and coordinating the interactions of a group of objects. The objects only know the mediator, thereby reducing the number of interconnections.

Page 18: Design Pattern lecture 4

Mediator DP (object - Behavioral DP)

Page 19: Design Pattern lecture 4

Mediator DP (object - Behavioral DP)

Page 20: Design Pattern lecture 4

Mediator DP (object - Behavioral DP)

Page 21: Design Pattern lecture 4

Mediator DP (object - Behavioral DP)

Page 22: Design Pattern lecture 4

Mediator DP (object - Behavioral DP)

Page 23: Design Pattern lecture 4

Mediator DP (object - Behavioral DP)

Page 24: Design Pattern lecture 4

•Façade make requests to the subsystem, while mediator enables cooperative behaviour between colleagues objects

Mediator DP (object - Behavioral DP)

Page 25: Design Pattern lecture 4

• What• capture and externalize an object's internal state

so that the object can be restored to this state later.

• Where • To store a snapshot of internal state of an object,

to implement checkpoints

•Why• To implement checkpoints, save state, undo

mechanisms

• How• use passive class to get and set state of Originator

class

MeMento DP (object - Behavioral DP)

Page 26: Design Pattern lecture 4

MeMento DP (object - Behavioral DP)

Page 27: Design Pattern lecture 4

MeMento DP (object - Behavioral DP)

Page 28: Design Pattern lecture 4

•Memento objects are passive

•Memento can be used to maintain state for undoable operations for Command DP

•Used in Games to store check points

MeMento DP (object - Behavioral DP)

Page 29: Design Pattern lecture 4

Observer DP (object - Behavioral DP)

Page 30: Design Pattern lecture 4

• What• An Observer pattern is a design based on a one-to-many

relationship, where one is a publisher that publishes an event against which many subscribers register an interest.

• Where • A requirement to initiate and manage communications among a

society of objects.

• Why• To programmatically establish and manage a set of relationships

among objects at run time.

• How• Create a subject object (publisher) and any number of observer

objects (subscribers), and then wire an event handler in the observer objects to an event in the subject object. In .NET, we use a delegate event-handling model to wire the observer objects to the publisher's event—delegates simplify the architecture articulated by GoF.

Observer DP (object - Behavioral DP)

Page 31: Design Pattern lecture 4

Observer DP (object - Behavioral DP)

Page 32: Design Pattern lecture 4

Observer DP (object - Behavioral DP)

Page 33: Design Pattern lecture 4

Observer DP (object - Behavioral DP)

Page 34: Design Pattern lecture 4

State DP (object - Behavioral DP)

• What• Allow an object to alter its behavior when its internal

state changes. The object will appear to change its class.

• Where • An object's behavior depends on its state, and it must

change its behavior at run-time depending on that state.

•Why• This lets you treat the object's state as an object in

its own right that can vary independently from other objects.

• How• Define state as an object on its own

Page 35: Design Pattern lecture 4

State DP (object - Behavioral DP)

Page 36: Design Pattern lecture 4

State DP (object - Behavioral DP)

Page 37: Design Pattern lecture 4

Strategy DP(object-behavioural DP)

• What• A design that presents a family of algorithms or business rules

encapsulated in classes that can be swapped.

• Where • A requirement for the contextual implementation of different

algorithms or business rules without the use of conditional code.

• Why• A design that separates the choice of algorithm or business

rule from its implementation and delegates the contextual choice of algorithm or business rule to client code.

• How• Design an abstract strategy class that includes an abstract

method from which an algorithm may be called. Prepare a context class to contain an abstract strategy class and then code the client to choose the strategy and inform the context class of the choice of strategy.

Page 38: Design Pattern lecture 4

Strategy DP(object-behavioural DP)

Page 39: Design Pattern lecture 4

Template Method DP(Class - Behavioral DP)

• What• Need subclasses to house different implementations

of an algorithm and defer part of the implementation to the subclass.

• Where• A requirement for a common structure to house an

algorithm, while vary the implementation of the algorithm.

• Why• A default implementation that has the flexibility for

a subclass to vary the underlying algorithm within the implementation.

• How• An abstract class exposes a Template Method that

wraps a set of functionality or methods, part of which is overridden by a subclass.

Page 40: Design Pattern lecture 4

Template Method DP(Class - Behavioral DP)

Page 41: Design Pattern lecture 4

Template Method DP(Class - Behavioral DP)

Page 42: Design Pattern lecture 4

• Factory Methods are often called by template methods. (DoCreateDocument called by OpenDocument).

• Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.

Template Method DP(Class - Behavioral DP)

Page 43: Design Pattern lecture 4

Report #3:

Interpreter DPVisitor DP

N.B. Hand Written

Page 44: Design Pattern lecture 4

References

[1] Design Patterns, Christopher G. Lasater, Wordware Publishing, Inc.

[2] http://www.CodeProject.com, How I explained OOD to my wife, Al-Farooque Shubho

[3]http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod,PrinciplesOfOod

[4]http://www.CodeProject.comHow I explained Design Patterns to my wife: Part 1,By Al-FarooqueShubho

[5] http://www.shubho.net/2010/09/interface-vs-abstract-class-classic.html, Al-FarooqueShubho

[6]C# 3.0 Design Patterns, Judith Bishop

[7]Head First Design Pattern, Eric Freeman, Elisabeth Freeman, Kathy Sierra, Bert Bates

[8] Pro .NET 2.0 Code and Design Standards in C#, Mark Horner

Page 45: Design Pattern lecture 4

The End

Thank you

and

Good Luck