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.

Slides:



Advertisements
Similar presentations
MicroKernel Pattern Presented by Sahibzada Sami ud din Kashif Khurshid.
Advertisements

Transactions and Recovery Checkpointing Souhad Daraghma.
Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
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,
March Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
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.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Feb Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface the client expects. An adapter lets classes work.
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.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Command Pattern When Actions Speak Louder Than Words.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Accelerated Computer Technologies Company Overview.
Transactions and Recovery
Academic Year 2014 Spring. MODULE CC3005NI: Advanced Database Systems “DATABASE RECOVERY” (PART – 1) Academic Year 2014 Spring.
BY VEDASHREE GOVINDA GOWDA
CIS—100 Chapter 13--Outlook 1. Using Your Mail to Take Action 2 Outlooks allows you to take several actions to organize, categorize, and respond to your.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
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.
SAMANVITHA RAMAYANAM 18 TH FEBRUARY 2010 CPE 691 LAYERED APPLICATION.
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.
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
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.
New features for CORBA 3.0 by Steve Vinoski Presented by Ajay Tandon.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
DAAD project “Joint Course on OOP using Java” On Object Oriented modeling in Java (Why & How) Ana Madevska Bogdanova Institute of informatics Faculty of.
Lexi case study (Part 2) Presentation by Matt Deckard.
Facade Introduction. Intent Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the.
1 Command Design Pattern Rick Mercer. Command Design Pattern The Command Pattern encapsulates a request as an object, thereby letting you queue commands,
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Computer Science 313 – Advanced Programming Topics.
آرمان حسين‌زاده آذر  Access to data varies depending on the source of the data.  Access to persistent storage, such as to a database, varies greatly.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its.
Distributed File Systems 11.2Process SaiRaj Bharath Yalamanchili.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
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.
I Like Ike I have always found that plans are useless, but planning is indispensable.
CSE 432: Thanks for the memory leaks, Pushme-Pullyu, and Command Summary of “Thanks for the Memory Leaks” “Left-over” design forces from Type Laundering.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Proxy. PBA WEB – BEWP 2 The Proxy pattern What is a Proxy? A Proxy is a ”placeholder” for a different object, to which we will not allow direct access.
The Command Pattern SE-2811 Dr. Mark L. Hornick 1.
Command Pattern Encapsulation Invocation. One size fits all.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
A service Oriented Architecture & Web Service Technology.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Turning method call into an object
MPCS – Advanced java Programming
Behavioral Design Patterns
Singleton Pattern Command Pattern
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Command Pattern.
Programming Design Patterns
Doug Jeffries CS490 Design Patterns May 1, 2003
Command Pattern 1.
PH Chapter 3 Thanks for the Memory Leaks Pushme-Pullyu (pp
Software Design Lecture : 35.
The Command Design Pattern
Software Engineering and Architecture
Presentation transcript:

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 a command? –Who executes a command? –Who knows the details of the command?

RHS – SWC 3 Executing a command Details to consider (continued): –When should the command be executed? –Should the command return a value? –Should we be able to undo a command? –What about sets of commands?

RHS – SWC 4 Executing a command Parties involved in a command –Client: formulates the command –Invoker: sets the ”wheels in motion” for getting the command executed –Manager: receives the command from the invoker –Worker: Actually executes the command

RHS – SWC 5 Simple commands Simple execution of a command: –Client and Invoker are the same –Manager and Worker are the same –Everybody knows all details of the command –Command executed immediately by Worker –Command does not return a value –Command cannot be undone –Command stands alone

RHS – SWC 6 Simple commands Get a shave, now!! Sir, right away, sir!! Client and Invoker Manager and Worker

RHS – SWC 7 Simple commands A simple command is similar to a method call Not really any reason – or opportunity – for reducing coupling For more advanced systems, this is not flexible enough

RHS – SWC 8 Complex commands Consider how an order is processed in a restaurant –Customer: decides which menu item to order –Waiter: picks up orders from Customers, and brings them to the Kitchen manager –Kitchen manager: keeps track of incoming orders, and distributes them to Chefs –Chef: Prepares the order

RHS – SWC 9 Complex commands Customer Waiter Kitchen Manager Chef

RHS – SWC 10 Complex commands If the scheme for simple commands was used, customers should yell orders directly to chefs… System would break down quickly By turning commands into objects, we gain much more flexibility

RHS – SWC 11 Complex commands Using objects for command enables –Queueing of commands – we can postpone execution of a command –Undoing of commands –Logging of commands –Better separation of knowledge and responsibility – decoupling!

RHS – SWC 12 Complex commands A Customer in a restaurant decides which order to prepare – but does not care about the details of preparation The Waiter and Kitchen Manager do not really care about details either The Chef, however, must know all details Encapsulate what varies…

RHS – SWC 13 Complex commands In OO terms, we therefore need an interface for commands Interface for all commands is the same, but implemen- tation is of course specific for each command Command execute()

RHS – SWC 14 Complex commands Command execute() MakePancakes execute() MakeSteak execute() Contains all details about making pancakes

RHS – SWC 15 Complex commands Customer wants pancakes, so he creates a MakePancakes object Waiter takes order to Kitchen Manager, but only knows that it is a Command object (think numbers for an order…)

RHS – SWC 16 Complex commands Kitchen Manager can manage the object (queue it, etc.) but also only knows the interface When appropriate, the Kitchen Manager calls execute on the Command object Code in Command object is then executed (by a Chef)

RHS – SWC 17 Complex commands Customer Waiter Kitchen Manager Chef Command order = new MakePancakes(…); manager.send(order); order.execute(); // Do execute() for MakePancakes

RHS – SWC 18 The Command pattern Client InvokerManager Worker Command Concrete- Command

RHS – SWC 19 The Command pattern The Client can create specific commands The Invoker and Manager only knows the Command interface The Worker carries out the actual work defined in the command Further flexibility can be introduced by making a Worker interface

RHS – SWC 20 The Command pattern Worker action(Command c) ConcreteWorker action(Command c) Command setWorker(Worker w) execute() ConcreteCommand setWorker(Worker w) execute()

RHS – SWC 21 The Command pattern // Client code Worker w = new MergeSortWorker(); Command c = new SortBankAccounts(); c.setWorker(w); theInvoker.invokeCommand(c);... // Code from Command public void execute() { theWorker.action(this); }

RHS – SWC 22 Queueing of commands Queueing is used when immediate execution of a command cannot be guaranteed Command is executed when needed resources are available Queue of command is managed by the Manager

RHS – SWC 23 Queueing of commands Typical example of a ”producer/consumer” scenario, using threads: Command … Producer thread Consumer thread Consumer thread Consumer Thread(s) pus h pop

RHS – SWC 24 Undo of commands Is – in principle – very easy; just let concrete Command classes implement an undo method, which has the reverse effect of the execute method: public void execute() { light.On(); } public void undo() { light.Off(); }

RHS – SWC 25 Undo of commands Simple or complex command – the specific Command class always knows how to undo an action. Somebody (Manager?) must keep track of which action to undo…

RHS – SWC 26 Logging of commands Primary application is for crash recovery Add two new methods Store and Load to the Command interface. Store : Save the state of the command to disk Load : Load the state on the command into the command. Calling Execute() will now execute the reestablished command.

RHS – SWC 27 Logging of commands Could call Store as part of Execute, or at certain externally defined ”checkpoints”. Important use is for transactional proces- sing, where e.g. system crash must not change the execution of the command Z Z Z