Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A part of a university lecture series on software engineering. Lecture 5 focuses on the gof design patterns, specifically the behavioral patterns memento and observer. The memento pattern allows an object to restore its internal state without violating encapsulation. The observer pattern defines a one-to-many dependency between objects, allowing automatic updates when one object changes state.
Typology: Slides
1 / 24
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 3
Sharif University of Technology 4
It may be difficult in some
^ Define a one-to-many dependency between objects so that
y^ p^ y^
j when one object changes state, all its dependents are notifiedand updated automatically. Department of Computer Engineering^
Sharif University of Technology 6
^ 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
Sharif University of Technology 9
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
tt^ h ^ Use the State pattern when^ ^ An object's behavior depends on its state
and it
^ An object's behavior depends on its state, and itmust change its behavior at run-time depending onthat state. ^ Operations have large, multipart conditionalstatements that depend on the object's statestatements that depend on the object's state.Department of Computer Engineering^
Sharif University of Technology 12
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
^ Define a family of algorithms, encapsulate each one, and makey^
g^ ,^ p^
them interchangeable. Strategy lets the algorithm varyindependently from clients that use it. Department of Computer Engineering^
Sharif University of Technology 15
^ 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
Sharif University of Technology 18
^ 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
Sharif University of Technology 21
Sharif University of Technology 22
Sharif University of Technology 23
R^ J h^ R^ d Vli
id^ J^ D^ i ^ Gamma, E., Helm, R., Johnson, R., and Vlissides, J.,
Design Patterns: Elements of Reusable Object-oriented Software.Addison-Wesley, 1995.Department of Computer Engineering^
Sharif University of Technology 24