
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















Department of Computer Engineering
Sharif University of Technology^1
^ 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
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
Sharif University of Technology 4
It may be difficult in some
^ 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
Sharif University of Technology 8
subscribed to it. 8 Unexpected updates^
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
^ 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
Sharif University of Technology 13
l 9 It makes state transitions explicit. 9 State objects can be shared.Department of Computer Engineering
Sharif University of Technology^14
^ 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
Sharif University of Technology 17
^ 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
^ an object structure contains many classes of objects withdiffering interfaces, and you want to perform operationsth^ bj^ t^
many distinct and unrelated operations need to beperformed on objects in an object structure
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