Command Pattern 1.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

Command Explained. Intent Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests,
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Computer Science 313 – Advanced Programming Topics.
More Interfaces, Dynamic Binding, and Polymorphism Kirk Scott.
Command Pattern 1. Intent Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log request,
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
March Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
William Shiver.  Pattern used to decouple an abstraction from its implementation so that the two can vary independently.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
1 Pattern Games CS : Software Design Winter /T9.
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.
Feb Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface the client expects. An adapter lets classes work.
ECE 355 Design Patterns Tutorial Part 3 Presented by Igor Ivković
Command Pattern Chihung Liao Cynthia Jiang. Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Cook Make Food()
Spring 2010ACS-3913 Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.
Command Pattern When Actions Speak Louder Than Words.
CS 350 – Software Design Command Object Remote Control Object.
BY VEDASHREE GOVINDA GOWDA
Object Oriented Software Development
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
CS 210 Introduction to Design Patterns September 28 th, 2006.
Team 5: The Infinite Loops Gloria Berumen Patricia Martinez Jose Roberto Salcido Michelle Soto Jose Luis Yanez Omar Zorrilla Design Pattern.
Introduction to Object-Oriented Programming
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Behavioral Pattern: Command C h a p t e r 5 – P a g e 139 There are times when the need arises to issue a request to an object without knowing anything.
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
Command. RHS – SOC 2 Executing a command Executing a command appears simple at first, but many details to consider: –Who creates a command? –Who invokes.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.
1 Command Design Pattern Rick Mercer and Rice University.
Lexi case study (Part 2) Presentation by Matt Deckard.
CS 210 Introduction to Design Patterns September 26 th, 2006.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
1 Command Design Pattern Rick Mercer. Command Design Pattern The Command Pattern encapsulates a request as an object, thereby letting you queue commands,
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Computer Science 313 – Advanced Programming Topics.
CS 210 Review Session October 5 th, Head First Design Patterns Chapter 4 Factory Pattern.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
Singleton Pattern Presented By:- Navaneet Kumar ise
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.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
I Like Ike I have always found that plans are useless, but planning is indispensable.
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.
Command Pattern Encapsulation Invocation. One size fits all.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
GC211 Data structure Lecture 3 Sara Alhajjam.
Façade Pattern:.
Behavioral Design Patterns
Software Design and Architecture
More Interfaces, Dynamic Binding, and Polymorphism
Singleton Pattern Command Pattern
Command Pattern.
Programming Design Patterns
Command Design Pattern
State Design Pattern 1.
Introduction to Behavioral Patterns (2)
Behavioral Patterns Part-I introduction UNIT-VI
PH Chapter 3 Thanks for the Memory Leaks Pushme-Pullyu (pp
BRIDGE PATTERN.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Chapter 9 Carrano Chapter 10 Small Java
ECE 355 Design Patterns Tutorial Part 3
The Command Design Pattern
Presentation transcript:

Command Pattern 1

Intent Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log request, and support undoable operations

Command Pattern An object is used to represent and encapsulate all the information needed to call a method at a later time This information includes the method name, the object that owns the method, and the values for the method parameters Three fundamental Command Pattern terms: Client : instantiates the Command Object and provides information to call the method at a later time Invoker : decides which method should be called (Executes the commands possibly at a later time) Receiver : an instance of the class that contains the method’s code (the object the command should affect)

Class Diagram <<interface>> Command Client Invoker execute() undo () setCommand () Receiver ConcreteCommand action() execute() undo () Receiver.Action ()

Collaborations The Client is responsible for creating a ConcreteCommand and setting its Receiver The Receiver knows how to perform the work needed to carry out the request. Any class can act as a Receiver. The Invoker holds a ConcreteCommand and at some point asks the ConcreteCommand to carry out a request by calling its execute () method Command declares an interface for all commands The ConcreteCommand defines a binding between an action and a Receiver. The Invoker makes a request by calling execute () and the ConcreteCommand carries it out by calling one or more actions on the Receiver.

Sequence Diagram A Receiver A Client A Command An Invoker new Command (Receiver) StoreCommand (Command) Execute () Action ()

Class Diagram <<interface>> Command Client execute() Switch (invoker) Client on() off() execute() (Application) Light (Receiver) LightOnCommand turnOn() turnOff() execute() myLight.turnOn()

Implementation (Command) public interface ICommand { void execute (); }

Implementation (invoke) public class RemoteSwitch { private ICommand onCommand; private ICommand offCommand; public RemoteSwitch (ICommand Up, ICommand Down) onCommand = Up; offCommand = Down; } public void on () { onCommand.execute (); } public void off () { offCommand.execute (); }

Implementation (receiver [light]) public class Light { public void turnOn() System.Debug.WriteLine (“Light is on”); } public void turnOff() System.Debug.WriteLine (“Light is off”);

Implementation (ConcreteCommand) public class LightOnCommand : ICommand { private Light myLight; public LightOnCommand (Light L) { myLight = L; } public void execute() { myLight.turnOn (); } } public class LightOffCommand : ICommand public LightOffCommand (Light L) { myLight.turnOff (); }

Implementation (receiver [fan]) public class Fan { public void startRotate () System.Debug.WriteLine (“Fan is rotating”); } public void stopRotate () System.Debug.WriteLine (“Fan is not rotating”);

Implementation (ConcreteCommand) class FanOnCommand : ICommand { private Fan myFan; public FanOnCommand (Fan F) { myFan = F; } public void execute() { myFan.startRotate (); } } class FanOffCommand : ICommand public FanOffCommand (Fan F) { myFan.stopRotate (); }

Implementation (client) public class TestCommand { public static void Main (String[] args) Light light = new Light (); LightOnCommand lightOn = new LightOnCommand (light); LightOffCommand lightOff = new LightOffCommand (light); RemoteSwitch remoteSwitch1 = new RemoteSwitch (lightOn, lightOff); remoteSwitch1.on (); remoteSwitch1.off (); Fan fan = new Fan(); FanOnCommand fanOn = new FanOnCommand (fan); FanOffCommand fanOff = new FanOffCommand (fan); RemoteSwitch remoteSwitch2 = new RemoteSwitch (fanOn, fanOff); remoteSwitch2.on (); remoteSwitch2.off (); }

Main Concepts It decouples an object making a request, from the one that knows how to perform it The command requester only needs to know how to issue it, it doesn’t need to know how to perform it Command object is at the center of this decoupling and encapsulates a receiver with an action An invoker makes a request of a Command object by calling its execute() method, which invokes those actions on the receiver

?

References Hyder, Shahriar. “Command Design Pattern”