patterns in software engineeringsoftware engineering...

27
Patterns in Software Engineering Software Engineering Lecturer: Raman Ramsin Lecturer: Raman Ramsin Lecture 4 GoF Design Patterns – Behavioral P t1 Department of Computer Engineering 1 Sharif University of Technology Part 1

Upload: buikiet

Post on 08-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

Patterns inSoftware EngineeringSoftware Engineering

Lecturer: Raman RamsinLecturer: Raman Ramsin

Lecture 4

GoF Design Patterns – BehavioralP t 1

Department of Computer Engineering1

Sharif University of Technology

Part 1

Patterns in Software Engineering – Lecture 4

GoF Behavioral Patterns – Class

Class

Interpreter: Given a language, define a representation for its grammar along with an interpreter that uses the representation g g p pto interpret sentences in the language.

Template Method: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses; lets subclasses redefine certain steps of an algorithm without changing the algorithm's structurealgorithm s structure.

Department of Computer Engineering2

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

GoF Behavioral Patterns – Object

Object

Chain of Responsibility: Avoid coupling the sender of aChain of Responsibility: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Command: Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Iterator: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Mediator: Define an object that encapsulates how a set of objects interact; promotes loose coupling by keeping objects from referring to each other explicitly.

Department of Computer Engineering3

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

GoF Behavioral Patterns – Object (Contd.)

Object (Contd.)

M t With t i l ti l ti t dMemento: Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

Observer: Define a one to many dependency between objectsObserver: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

State: Allow an object to alter its behavior when its internalState: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

Strategy: Define a family of algorithms, encapsulate each one, and make them interchangeable; lets the algorithm varyand make them interchangeable; lets the algorithm vary independently from clients that use it.

Visitor: Represent an operation to be performed on the elements of an object structure; lets you define a new

Department of Computer Engineering4

Sharif University of Technology

elements of an object structure; lets you define a new operation without changing the classes of the elements.

Patterns in Software Engineering – Lecture 4

Chain of Responsibility

Intent:Avoid coupling the sender of a request to its receiver by giving moreAvoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Department of Computer Engineering5

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Chain of Responsibility: Class Hierarchy

Department of Computer Engineering6

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Chain of Responsibility: Applicability

Use the Chain of Responsibility pattern whenUse the Chain of Responsibility pattern when

more than one object may handle a request, and the j y q ,handler isn't known a priori. The handler should be ascertained automatically.

you want to issue a request to one of several objects without specifying the receiver explicitly.

the set of objects that can handle a request should be specified dynamically.

Department of Computer Engineering7

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Chain of Responsibility: Structure

Department of Computer Engineering8

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Chain of Responsibility: Consequences

Reduced coupling.

Added flexibility in assigning responsibilities to objects.

Receipt isn't guaranteed.

Department of Computer Engineering9

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Command

Intent:Encapsulate a request as an object, thereby letting you parameterize clients with different requests queue or log requests and supportclients with different requests, queue or log requests, and support undoable operations.

Department of Computer Engineering10

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Command: Examples

Department of Computer Engineering11

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Command: Macro-Command

Department of Computer Engineering12

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Command: Applicability

Use the Command pattern when you want to

parameterize objects by an action to perform.

specify, queue, and execute requests at different times.

support undo. The Command's Execute operation can store state for reversing its effects in the command itself. The Command interface must have an added Unexecute operation that reverses the effects of a previous Executethat reverses the effects of a previous Execute.

support logging changes so that they can be reapplied in case of a system crash (by augmenting the Command interface with load and store operations)load and store operations).

structure a system around high-level operations built on primitives operations (based on the commands' common i f )

Department of Computer Engineering13

Sharif University of Technology

interface).

Patterns in Software Engineering – Lecture 4

Command: Structure

Department of Computer Engineering14

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Command: Collaboration

Department of Computer Engineering15

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Command: Consequences

Command decouples the object that invokes the p joperation from the one that knows how to perform it.

Commands are first-class objects They can beCommands are first class objects. They can be manipulated and extended like any other object.

Y bl d i iYou can assemble commands into a composite command.

It's easy to add new Commands, because you don't have to change existing classes.

Department of Computer Engineering16

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Iterator

Intent:Provide a way to access the elements of an aggregate objectProvide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Department of Computer Engineering17

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Iterator: Example

Department of Computer Engineering18

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Iterator: Applicability

U th It t ttUse the Iterator pattern

to access an aggregate object's contents without exposing its linternal representation.

to support multiple traversals of aggregate objectsto support multiple traversals of aggregate objects.

to provide a uniform interface for traversing differentto provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration).

Department of Computer Engineering19

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Iterator: Structure

Department of Computer Engineering20

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Iterator: Consequences

It t i ti i th t l f tIt supports variations in the traversal of an aggregate.

Iterators simplify the Aggregate interface. Iterator's traversal interface obviates the need for a similar interface in Aggregate, thereby simplifying its interface.

More than one traversal can be pending on an aggregate.More than one traversal can be pending on an aggregate. You can have more than one traversal in progress at once.

Department of Computer Engineering21

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Mediator

Intent:Define an object that encapsulates how a set of objects interact:Define an object that encapsulates how a set of objects interact: promotes loose coupling by keeping objects from referring to each other explicitly, and lets you vary their interaction independently.

Department of Computer Engineering22

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Mediator: Typical Collaboration and Class Hierarchy

Department of Computer Engineering23

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Mediator: Applicability

Use the Mediator pattern whenUse the Mediator pattern when

a set of objects communicate in well-defined but jcomplex ways. The resulting interdependencies are unstructured and difficult to understand.

reusing an object is difficult because it refers to and communicates with many other objects.

a behavior that's distributed between several classes should be customizable without a lot of subclassing.

Department of Computer Engineering24

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Mediator: Structure

Department of Computer Engineering25

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

Mediator: Consequences

It limits subclassing. A mediator localizes behavior that otherwise would be distributed among several objects Changing this behaviorwould be distributed among several objects. Changing this behavior requires subclassing Mediator only.

It decouples colleagues. A mediator promotes loose coupling between colleaguesbetween colleagues.

It simplifies object protocols.

It abstracts how objects cooperate. Making mediation anIt abstracts how objects cooperate. Making mediation an independent concept and encapsulating it in an object lets you focus on how objects interact apart from their individual behavior.

It centralizes control. The Mediator pattern trades complexity of interaction for complexity in the mediator.

Department of Computer Engineering26

Sharif University of Technology

Patterns in Software Engineering – Lecture 4

ReferenceReference

G E H l R J h R d Vli id J D iGamma, E., Helm, R., Johnson, R., and Vlissides, J., Design Patterns: Elements of Reusable Object-oriented Software.Addison-Wesley, 1995.

Department of Computer Engineering27

Sharif University of Technology