Command Pattern 1. Intent Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log request,

Slides:



Advertisements
Similar presentations
Command Explained. Intent Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests,
Advertisements

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.
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.
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
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
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.
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.
Design Patterns Introduction
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.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
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.
Chapter 8, Object Design: Reuse and Patterns III
Examples of Classes & Objects
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 Pattern 1.
Command Design Pattern
Introduction to Behavioral Patterns (2)
Classes & Objects: Examples
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 2

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) 3

Class Diagram 4 ClientInvoker setCommand () Receiver action() ConcreteCommand execute() undo () > Command 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. 5

Sequence Diagram 6 A ReceiverA ClientA CommandAn Invoker new Command (Receiver) StoreCommand (Command) Execute () Action ()

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

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

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

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

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

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

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 remoteSwitch = new RemoteSwitch (lightOn, lightOff); remoteSwitch.on (); remoteSwitch.off (); Fan fan = new Fan(); FanOnCommand fanOn = new FanOnCommand (fan); FanOffCommand fanOff = new FanOffCommand (fan); RemoteSwitch remoteSwitch = new RemoteSwitch (fanOn, fanOff); remoteSwitch.on (); remoteSwitch.off (); } 13

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

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 15

References 17 Hyder, Shahriar. “Command Design Pattern”