Docsity

Software Engineering Lecture 5: GoF Design Patterns - Behavioral (Memento and Observer), Slides of Software Engineering

Partial preview of the text

Download Software Engineering Lecture 5: GoF Design Patterns - Behavioral (Memento and Observer) and more Slides Software Engineering in PDF only on Docsity!

Patterns inSoftware EngineeringSoftware Engineering Lecturer: Raman RamsinLecturer:^ Raman RamsinLecture 5^ GoF Design Patterns โ€“ Behavioral

P^ t 2

Department of Computer Engineering

Sharif University of Technology^1

Part 2

Patterns in Software Engineering โ€“ Lecture 5 Memento ย„ Intent:^

^ Without violating encapsulation, capture and externalize anobject's internal state so that the object can be restored tothis state later. ย„ Applicability:U^ th^ M^ t

tt^ h

Use the Memento pattern when ย„ a snapshot of (some portion of) an object's state mustbe saved so that it can be restored to that state later,dand ย„ a direct interface to obtaining the state would exposeimplementation details and break the object'sl ti

Department of Computer Engineering encapsulation.^ Sharif University of Technology^2

Patterns in Software Engineering โ€“ Lecture 5 Memento: Collaboration Department of Computer Engineering^

Sharif University of Technology 4

Patterns in Software Engineering โ€“ Lecture 5 Memento: Consequences 9 Preserving encapsulation boundaries. The pattern shields otherobjects from potentially complex Originator internalsobjects from potentially complex Originator internals. 9 It simplifies Originator. Having clients manage the state they ask forsimplifies Originator and keeps clients from having to notifyoriginators when they're done.originators when they re done. 8 Using mementos might be expensive. Mementos might incurconsiderable overhead if Originator must copy large amounts ofconsiderable overhead if Originator must copy large amounts ofinformation to store in the memento or if clients create and returnmany mementos. 8 Defining narrow and wide interfaces.

It may be difficult in some

Defining^ narrow and wide interfaces. It may be difficult in somelanguages to ensure that only the originator can access thememento's state. 8 Hidden costs in caring for mementos.Department of Computer Engineering^

g Sharif University of Technology^5

Patterns in Software Engineering โ€“ Lecture 5 Observer: Applicability ย„ Use the Observer pattern whenย„ Use^ the Observer pattern when^

^ an abstraction has two aspects, one dependent on the other.Encapsulating these aspects in separate objects lets you varyEncapsulating these aspects in separate objects lets you varyand reuse them independently.^ ^ a change to one object requires changing others

and you don't

^ a change to one object requires changing others, and you don tknow how many objects need to be changed. ^ an object should be able to notify other objects without making ^ an object should be able to notify other objects without makingassumptions about who these objects are. In other words, youdon't want these objects tightly coupled.Department of Computer Engineering^

Sharif University of Technology 7

Patterns in Software Engineering โ€“ Lecture 5 Observer: Structure Department of Computer Engineering^

Sharif University of Technology 8

Patterns in Software Engineering โ€“ Lecture 5 Observer: Consequences 9 Abstract coupling between Subject and Observer 9 Abstract^ coupling between Subject and Observer. 9 Support for broadcast communication. The notification isbroadcast automatically to all interested objects thatb oadcast auto^ at ca y to a

te ested objects t at

subscribed to it. 8 Unexpected updates^

Because observers have no knowledge

8 Unexpected updates. Because observers have no knowledgeof each other's presence, they can be blind to the ultimatecost of changing the subject.^8 A seemingly innocuous operation on the subject may cause acascade of updates to observers and their dependent objects.Department of Computer Engineering

Sharif University of Technology^10

Patterns in Software Engineering โ€“ Lecture 5 State ย„ Intent:^

^ All^ bj^ t t^

lt^ it^ b h^ i^ h^ it

i t^ l^ t t

^ Allow an object to alter its behavior when its internal statechanges. The object will appear to change its class. Department of Computer Engineering^

Sharif University of Technology 11

Patterns in Software Engineering โ€“ Lecture 5 State: Structure Department of Computer Engineering^

Sharif University of Technology 13

Patterns in Software Engineering โ€“ Lecture 5 State: Consequences 9 It localizes state-specific behavior and partitions behaviorfor different states. New states and transitions can beadded easily by defining new subclassesadded easily by defining new subclasses. 9 k

l 9 It makes state transitions explicit. 9 State objects can be shared.Department of Computer Engineering

Sharif University of Technology^14

Patterns in Software Engineering โ€“ Lecture 5 Strategy: Applicability ย„ Use the Strategy pattern when^

^ many related classes differ only in their behavior. Strategiesprovide a way to configure a class with one of many behaviors.^ ^ you need different variants of an algorithm. For example, youmight define algorithms reflecting different space/time trade-offs.^ ^ an algorithm uses data that clients shouldn't know about. Usethe Strategy pattern to avoid exposing complex, algorithm-specific data structuresspecific data structures.^ ^ a class defines many behaviors, and these appear as multipleconditional statements in its operations.Department of Computer Engineering^

Sharif University of Technology 16

Patterns in Software Engineering โ€“ Lecture 5 Strategy: Structure Department of Computer Engineering^

Sharif University of Technology 17

Patterns in Software Engineering โ€“ Lecture 5 Visitor ย„ Intent:^

^ Represent an operation to be performed on the elements of an objectstructure; lets you define a new operation without changing the;^ y^

p^ g^ g classes of the elements on which it operates. Department of Computer Engineering^

Sharif University of Technology 19

Patterns in Software Engineering โ€“ Lecture 5 Visitor: Applicability ย„ Use the Visitor pattern when^

^ an object structure contains many classes of objects withdiffering interfaces, and you want to perform operationsth^ bj^ t^

th t d^ d^ th i^

t^ l

on these objects that depend on their concrete classes.

many distinct and unrelated operations need to beperformed on objects in an object structure

and you

performed on objects in an object structure, and youwant to avoid "polluting" their classes with theseoperations.

the classes defining the object structure rarely change,but you often want to define new operations over thestructure.Department of Computer Engineering^

Sharif University of Technology 20

structure.