ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.

Slides:



Advertisements
Similar presentations
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Chapter 7 – Object-Oriented Design
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Computer Monitoring System for EE Faculty By Yaroslav Ross And Denis Zakrevsky Supervisor: Viktor Kulikov.
Spring 2010ACS-3913 Ron McFadyen1 Weather Station Page 39+ In this application, weather station devices supply data to a weather data object. As the data.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Copyright © Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-1 PS95&96-MEF-L16-1 Dr. M.E. Fayad Creationa l.
Command Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Memento Page 283 Jason Penny. Memento Behavioral Pattern Behavioral Pattern Intent: Intent: Without violating encapsulation, capture and externalize an.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
Behavioral Pattern: Memento C h a p t e r 5 – P a g e 179 Objects frequently expose only some of their internal state using public methods, but there.
Client/Server Software Architectures Yonglei Tao.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
BY VEDASHREE GOVINDA GOWDA
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
© Spiros Mancoridis 27/09/ Software Design Topics in Object-Oriented Design Patterns Material drawn from [Gamma95] and [Coplien95] Revised and augmented.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
Programming in C# Observer Design Pattern
Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Lexi case study (Part 2) Presentation by Matt Deckard.
1 Command Design Pattern Rick Mercer. Command Design Pattern The Command Pattern encapsulates a request as an object, thereby letting you queue commands,
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw.
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Jim Fawcett CSE776 – Design Patterns Summer 2005
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
Behavioral Design Patterns
Observer Design Pattern
Observer Design Pattern
Memento Pattern F 羅世東 F 李天彥 F 鄧義佐 F 林尚霖
Memento Design Pattern
Programming Design Patterns
Design Patterns - A few examples
PH Chapter 3 Thanks for the Memory Leaks Pushme-Pullyu (pp
The iterator and memento patterns
Memento Pattern 1.
Presentation transcript:

ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento

ECE450 - Software Engineering II2 Keeping on top of things Many modern software applications need to maintain consistency with data stored or modified elsewhere –A stock trading application that responds to fluctuations in the stock market –A web-based client that lets you know when you received new messages –An instant messenger client that keeps track of the status of your contacts in real time –A chart in a spreadsheet that changes whenever its data source changes –...a long etcetera... How can we design an application that ensures this data consistency without making the classes tightly coupled? –For example, the data objects in the spreadsheet should not need to know that you have a chart You could have a table, or a reference to the data in a text editor, or a different kind of chart, and the spreadsheet should not need to differentiate between all these types Ideas?

ECE450 - Software Engineering II3 Publish-subscribe mechanism The key to solve this problem is to identify that there are two elements: A subject and an observer –A subject may have any number of dependent observers –All observers will be notified whenever the subject undergoes a change in state –The subject, then, is the “publisher” of notifications... –...and it sends these notifications to all of its “subscribers”, without worrying about what those subscribers are The Observer pattern –Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically

ECE450 - Software Engineering II4 Structure and participants Subject –Knows its observers. Any number of Observer objects may observe a subject –Provides an interface for attaching and detaching Obsever objects Observer –Defines an updating interface for objects that should be notified of changes in a subject

ECE450 - Software Engineering II5 Structure and participants ConcreteSubject –Stores state of interest to ConcreteObserver objects –Sends a notification to its observers when its state changes ConcreteObserver –Maintains a reference to a ConcreteSubject object –Stores state that should stay consistent with the subject’s –Implements the Observer updating interface to keep its state consistent with the subject’s

ECE450 - Software Engineering II6 Applicability Use the Observer pattern in any of the following situations: –When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently –When a change to one object requires changing others, and you don’t know how many objects need to be changed –When an object should be able to notify other objects without making assumptions about who these objects are In other words, you don’t want these objects tightly coupled Using Java? You can use its Observer and Observable interfaces –...but they’ll consume an inheritance dimension Also applicable as user interface event “listeners” –For example, you want to know when the user moves the mouse or presses a key... –...so you “listen” to events applying the Observer pattern

ECE450 - Software Engineering II7 Consequences The Observer pattern lets you vary subjects and observers independently –It lets you add observers without modifying the subject or other observers Abstract coupling between Subject and Observer –All a subject knows is that it has a list of observers, each conforming to the simple interface of the abstract Observer class –The subject doesn’t know the concrete class of any observer Support for broadcast communication –Unlike an ordinary request, the notification that a subject sends need not specify its receiver It is broadcast automatically to all subscribed objects –You can add and remove observers at any time; it is up to the observer to handle or ignore notifications

ECE450 - Software Engineering II8 Implementation Push- and pull-models of observing –So far we have described a “pull” model: Subjects notify observers that they have been modified, but do not specify what the modification was Each observer is responsible for “pulling” the specific information they are interested in –The “push” model consists of having the subject send observers detailed information about the change, whether they want it or not –The pull model emphasizes the subject’s ignorance of its observers, but may be inefficient –The push model is more efficient, but compromises reuse, as the Subject classes make assumptions about Observer classes that might not always be true

ECE450 - Software Engineering II9 Moving on: Requests can be objects too! First problem: Sometimes it’s necessary to issue requests to objects without knowing anything about the operations being requested or the receiver of the request –For instance, user interface toolkits that include objects like buttons and menus –the system does not know what each will do, but it knows that they’ll do something Second problem: Implementing undo –Still the nightmare of many architects –Can make or break your software’s usability Both problems can be solved with one pattern: Command –Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations

ECE450 - Software Engineering II10 Structure and Participants Command –Declares an interface for executing an operation ConcreteCommand –Defines a binding between a Receiver object and an action –Implements Execute by invoking the corresponding operation(s) on Receiver

ECE450 - Software Engineering II11 Structure and Participants Client –Creates a ConcreteCommand object and sets its receiver Invoker –Asks the command to carry out the request Receiver –Knows how to perform the operations associated with carrying out a request. Any class may serve as a Receiver

ECE450 - Software Engineering II12 Applicability Use the Command pattern when you want to... –Parameterize objects by an action to perform –Specify, queue, and execute requests at different times A Command object can have a lifetime independent of the original request –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 call to Execute Executed commands are stored in a history list Unlimited-level undo and redo is achieved by traversing the list backwards and forwards calling Unexecute and Execute, respectively –Support logging changes so that they can be reapplied in case of a system crash Recovering from a crash involves reloading logged commands from disk and reexecuting them with the Execute operation –Structure a system around high-level operations built on primitives operations Common in information systems that support transactions –Note that you can construct macros of commands using the Composite pattern

ECE450 - Software Engineering II13 Consequences Command decouples the object that invokes the operation from the one that knows how to perform it Commands are first-class objects. They can be manipulated and extended like any other object You can assemble commands into a composite command (again, through Composite) It’s easy to add new Commands, because you don’t have to change existing classes

ECE450 - Software Engineering II14 Implementation Avoiding error accumulation in the undo process –Hysteresis can be a problem (that is, undo -> redo leading to slightly different state) –Errors can accumulate as commands are executed, unexecuted, and reexecuted repeatedly –It may be necessary to store more information in the command to ensure objects are restored to their original state –The Memento pattern (coming up!) can be applied to give the command access to this information without exposing the internals of other objects What happens if you try to undo an operation such as Print()? –You can’t unprint! –Could do nothing –...or raise an exception... –...or provide an isUndoable method that acts like an impermeable barrier... and optionally clear the history at each un-undoable call

ECE450 - Software Engineering II15 And now for a discussion on Memento! (You must see it if you haven’t –one of the smartest and most creative movies ever!)

ECE450 - Software Engineering II16 The other Memento... Sometimes it’s necessary to record the internal state of an object –Implementing checkpoints –Undo mechanisms You must save state information somewhere so that you can restore objects... –...but objects normally encapsulate some or all of their state, making it inaccessible to others! –Exposing this state would violate encapsulation Memento: –Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later A memento is an object that stores a snapshot of the internal state of another object –the memento’s originator. –The undo mechanism will request a memento from the originator –The originator initializes the memento with information of its current state –Only the originator can store and retrieve information from the memento

ECE450 - Software Engineering II17 Participants and structure Memento –Stores internal state of the Originator object –Protects against access by objects other than the originator Originator –Creates a memento containing a snapshot of its current internal state –Uses the memento to restore its internal state Caretaker –Is responsible for the memento’s safekeeping –Never operates on or examines the contents of a memento

ECE450 - Software Engineering II18 Applicability Use the Memento pattern when... –A snapshot of (some portion of) an object’s state must be saved so that it can be restored to that state later, and –a direct interface to obtaining the state would expose implementation details and break the object’s encapsulation

ECE450 - Software Engineering II19 Consequences Preserving encapsulation boundaries –Memento avoids exposing information that only an originator should manage but that must be stored nevertheless outside the originator Using mementos might be expensive –They will incur in considerable overhead if Originator must copy large amounts of information to store the memento or if clients create and return mementos to the originator often Defining narrow and wide interfaces –It may be difficult in some languages to ensure that only the originator can access the memento’s state In general, you want to make Memento an internal class to the originator, so that the Caretaker does not know how to read it