Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.

Slides:



Advertisements
Similar presentations
Chain of Responsibility Pattern Gof pp Yuyang Chen.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Command Explained. Intent Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests,
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
Computer Science 313 – Advanced Programming Topics.
Command Pattern 1. Intent Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log request,
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-1 PS95&96-MEF-L15-1 Dr. M.E. Fayad Creationa l.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Command Pattern Chihung Liao Cynthia Jiang. Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Cook Make Food()
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Prototype Pattern Intent:
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
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.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
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.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
CS 210 Introduction to Design Patterns September 28 th, 2006.
© 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.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Pattern Hatching - John Vlissides Pages 85 – 101 Todd Anderson
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.
02 - Behavioral Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Lexi case study (Part 2) Presentation by Matt Deckard.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Design Patterns Introduction
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:
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
The Command Pattern SE-2811 Dr. Mark L. Hornick 1.
Command. RHS – SWC 2 Executing a command Executing a command appears simple at first, but many details to consider: –Who creates a command? –Who invokes.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Review of last class. Design patterns CreationalStructuralBehavioral Abstract Factory Builder Factory Singleton etc. Adapter Bridge Composite Decorator.
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
Turning method call into an object
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Behavioral Design Patterns
Software Design and Architecture
Intent (Thanks to Jim Fawcett for the slides)
Command Pattern.
Programming Design Patterns
Command Pattern 1.
Design Patterns Satya Puvvada Satya Puvvada.
Behavioral Patterns Part-I introduction UNIT-VI
PH Chapter 3 Thanks for the Memory Leaks Pushme-Pullyu (pp
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Creational Patterns.
12. Command Pattern SE2811 Software Component Design
The Command Design Pattern
Software Engineering and Architecture
Presentation transcript:

Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern

Intent Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. Also Known as  Action, Transaction

Motivation(1) Sometimes it ’ s necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.  Like impl. of buttons, menus in user interface toolkits. The Command pattern lets toolkit objects make requests of unspecified application objects by turning the request itself into an object.

Motivation(2) The key of this pattern is an abstract Command class, which declares an interface for executing operations. Concrete Command subclasses specify a receiver-action pair by storing the receiver as an instance variable and by implementing Execute to invoke the request.

Motivation(3) Application creates menus and menuitems. Application also keep track of Document objects. Application Add(Doc) Menu Add( MenuItem ) MenuItem Clicked() Command Execute() Command ->Execute() Document Open() Close() Cut() Copy() Paste()

Motivation(4) PasteCommand supports pasting text from the clipboard into a Document Command Execute() PasteCommand Execute() document-> Paste() Document Open() Close() Cut() Copy() Paste()

Motivation(5) OpenCommand Command Execute() OpenCommand Execute() AskUser() name=AskUser(); doc = new Document(name) doc->open() Application Add(Document)

Motivation(6) Macro Command to execute a series of commands Command Execute() MacroCommand Execute() for all c in commands c->Execute

Motivation(7) The Command pattern decouples the object that invokes the operation from the one having the knowledge to perform it. We can replace commands dynamically. We can also support command scripting by composing commands into larger ones.

Applicability(1) Use this pattern when  parameterize objects by an action to perform. Commands are object-oriented replacement for callbacks in procedural language.  specify, queue, and execute requests at different time.  support undo. The Command ’ s Execute operation can store state for reversing its effects in the command itself.

Applicability(2) support logging changes so that they can be reapplied in case of a system crash. structure a system around high-level operations built on primitives operations.

Structure ClientInvokerCommand Execute() Receiver ConcreteCommand Execute() state Action()

Participants(1) 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 on Receiver.

Participants(2) Client(Application)  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.

Collaborations(1) The client creates a ConcreteCommand object and specifies its receiver. An Invoker object stores the ConcreteCommand object. The invoker issues a request by calling Execute on the command. When commands are undoable, ConcreteCommand stores state for undoing the command prior to invoking Execute. The ConcreteCommand object invokes operations on its receiver to carry out the request.

Collaborations(2) sequence diagram new Command(aReceiver) Action() StoreCommand(aCommand) aReceiver aClientaCommandanInvoker Execute()

Consequence 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. (MacroCommand). It ’ s easy to add new Commands, because you don ’ t have to change existing classes.

Implementation(1) How intelligent should a command be?  One extreme: merely defines a binding between a receiver and the actions that carry out the request.  Another extreme: it implements everything itself: useful when want to define commands that are independent of existing receiver. no suitable receiver exists, or when a command knows its receiver implicitly.  Somewhere between: commands that have enough knowledge to find their receiver dynamically.

Implementation(2) Supporting undo and redo  additional state needed, including:  The Receiver object.  the arguments to the operation performed on the receiver,  any original values in the receiver that can change as a result of handling the request.

Implementation(3) Supporting undo and redo(2)  The application needs a history list of commands that have been executed.  An undoable command might have to be copied before it can be placed on the history list.  Commands that must be copied before being placed on the history list act as prototypes.

Implementation(4) Avoiding error accumulation in the undo process.  Hysteresis can be a problem in ensuring a reliable, semantics-preserving undo/redo mechanism. Errors can accumulate.  Memento pattern can be applied to give command access to this information without exposing the internals of other objects.

Implementation(5) Using C++ templates  We can use templates to avoiding creating a Command subclass for every kind of action and receiver for commands that aren ’ t undoable don ’ t require arguments  Will be shown in the Sample Code section.

Sample Code(1) Abstract Command class class Command { public: virtual ~Command(); virtual void Execute() = 0; protected: Command(); }

Sample Code(2) OpenCommand class class OpenCommand : public Command{ public: OpenCommand(Application *); virtual void Execute(); protected: virtual const char* AskUser(); private: Application* _application; char* _response; }; OpenCommand::OpenCommand(Application *a) { _application = a; }

Sample Code(3) OpenCommand(continued) void OpenCommand::Execute(){ const char* name = AskUser(); if ( name != 0) { Document *document = new Document(name); _appplication->Add(document); document->Open(); }

Sample Code(4) PasteCommand class class PasteCommand : public Command { public: PasteCommand(Document *); virtual void Execute(); private: Document * _document; } PasteCommand::PasteCommand(Document* doc) { _document = doc; } void PasteCommand::Execute() { _document -> Paste(); }

Sample Code(5) template for simple command template class SimpleCommand : public Command{ public: typedef void (Receiver::*Action)(); SimpleCommand(Receiver* r, Action a): _receiver(r), _action(a){} virtual void Execute(); private: Action _action; Receiver* _receiver; }

Sample Code(6) template for simple command template void SimpleCommand ::Execute(){ (_receiver->*_action)(); }

Sample Code(7) Using template MyClass* receiver = new MyClass … command* aCommand = new SimpleCommand (receiver, &MyClass::Action); aCommand->Execute();

Sample Code(8) MacroCommand class class MacroCommand : public Command { public: MacroCommand(); virtual ~MacroCommand(); virtual void Add(Command *); virtual void Remove(Command *); virtual void Execute(); private: List * _cmds; }

Sample Code(9) Execute MacroCommand void MacroCommand::Execute() { LIstIterator i(_cmds); for(i.First(); !i.IsDone(); i.Next()) { Command *c = i.CurrentItem(); c->Execute(); }

Sample Code(10) void MacroCommand::Add(Command *c) { _cmds->Append(c); } void MacroCommand::Remove(Command *c) { _cmds->Remove(c); }

Related Patterns A Composite can be used to implement MacroCommands. A Memento can keep state the command requires to undo its effect. A command that must be copied before being placed on the history list acts as a Prototype.