Programming Design Patterns

Slides:



Advertisements
Similar presentations
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
Advertisements

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.
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 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.
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.
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.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Command Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Chapter 13: Object-Oriented Programming
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.
CS 350 – Software Design Command Object Remote Control Object.
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.
Copyright © 2007, Oracle. All rights reserved. Managing Concurrent Requests.
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
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.
CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
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.
Key Applications Module Lesson 21 — Access Essentials
How to Create a Document in Google Drive By Tressa Beckler.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Lexi case study (Part 2) Presentation by Matt Deckard.
CS 210 Introduction to Design Patterns September 26 th, 2006.
Concurrent Programming and Threads Threads Blocking a User Interface.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
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:
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.
Inventory Management – Inventory Processes
Project Management: Messages
Generics, Exceptions and Undo Command
Eclipse EHX System Logic Maestro
Turning method call into an object
Python Programming Module 3 Functions Python Programming, 2/e.
Behavioral Design Patterns
Chapter 2: System Structures
Singleton Pattern Command Pattern
Chapter 3: Using Methods, Classes, and Objects
Note 11 Command Pattern SE2811 Software Component Design
Adding Assignments and Learning Units to Your TSS Course
Command Pattern.
Doug Jeffries CS490 Design Patterns May 1, 2003
Command Pattern 1.
12. Command Pattern SE2811 Software Component Design
Microsoft Word - Formatting Pages
Behavioral and Structural Patterns
Chapter 2: System Structures
Introduction to Behavioral Patterns (1)
Introduction to Behavioral Patterns (2)
Menu item at a restaurant
Using JDeveloper.
PH Chapter 3 Thanks for the Memory Leaks Pushme-Pullyu (pp
12. Command Pattern SE2811 Software Component Design
Microsoft Office Excel 2003
The Command Design Pattern
Software Engineering and Architecture
Microsoft Excel 2007 – Level 2
Presentation transcript:

Programming Design Patterns Remote Control Suppose you have a remote control with 7 slots Each slot has an ON and OFF button The remote controls a Light Bulb and a Garage Opener Code can be loaded into each slot the code for each slot can be changed from time to time When a button is pressed, assume a method “buttonWasPressed” is invoked on the remote with the slot number passed in as a parameter Code for the Light bulb can turn it ON and OFF Code for the Garage Opener can open and close it. Design code for the Remote Control, Light Bulb, and Garage Opener Programming Design Patterns 11/8/2018 1 1

Programming Design Patterns Design Considerations Requester of the action: Remote Control Performer of the action: Light Bulb, Garage Door Decouple the requester from the action performer Programming Design Patterns 11/8/2018 2 2

Programming Design Patterns Code Design (1) Store a command object for each button The remote control should have no idea what the work is for each slot Note that the functionality of each slot can be re- programmed Remote control should know how to “talk” to the object loaded in a slot talk to object in the slot in the same way each time Programming Design Patterns 11/8/2018 3 3

Programming Design Patterns Code Design (2) Need standard API to allow code to be added to each slot should be able to call “setCommand()” on the Remote Control All the objects controlled by the Remote Control should have a common method execute() should implement the “Command Interface” Programming Design Patterns 11/8/2018 4 4

Programming Design Patterns Command Interface public interface Command { public void execute(); } Programming Design Patterns 11/8/2018 5 5

Programming Design Patterns LightOn Command public class LightOnCommand implements Command { // CLASSWORK // constructor that takes a Light object // implement the interface method and accordingly // invoke a method on the Light object } Programming Design Patterns 11/8/2018 6 6

Programming Design Patterns LightOn Command public class LightOnCommand implements Command { private Light light; public LightOnCommand(Light lightIn) { light = LightIn; } // execute calls specific methods on // the receiving object public void execute() { light.on(); Programming Design Patterns 11/8/2018 7 7

Programming Design Patterns SimpleRemoteControl public class SimpleRemoteControl { // CLASSWORK // store all the commands // code for buttonWasPressed() } Programming Design Patterns 11/8/2018 8 8

Programming Design Patterns SimpleRemoteControl public class SimpleRemoteControl { private Command slot; public SimpleRemoteControl() {..} public void setCommand(Command commandIn) { slot = commandIn; } // Note how the remote control is decoupled from // the object that performs the action. Execute is // called for all the devices that are contoled public void buttonWasPressed() { slot.execute(); Programming Design Patterns 11/8/2018 9 9

Programming Design Patterns RemoteControl with many slots CLASSWORK Extend code for RemoteControl to support multiple slots Programming Design Patterns 11/8/2018 10 10

Programming Design Patterns RemoteControl with many slots Extend code for RemoteControl to support multiple slots public class RemoteControl { Command[] onCommands; Command[] offCommands; public void onButtonWasPushed(int slot) { onCommands[slot].execute(); } // … Programming Design Patterns 11/8/2018 11 11

Programming Design Patterns Command Pattern: Definition Encapsulates a request as an object, thereby letting you to parameterize other objects with different requests, queue or log requests, and support undoable operations what does “thereby letting you parameterize other objects with different requests” mean? Homework How can you “log requests”? Undo operations: support the “undo” command Constructor is passed different parameters Log requests: use a queue Programming Design Patterns 11/8/2018 12 12

Programming Design Patterns Undo and MetaCommand How can you add code for an “undo” operation? How can you implement the Meta Command Operation? multiple commands with a single button on the remote CLASSWORK Programming Design Patterns 11/8/2018 13 13

Programming Design Patterns Undo public interface Command { public void execute(); public void undo(); } Should keep track of the last command in the RemoteControl a data member Command undoCommand; Write code for “undo” for LightOnCommand object Programming Design Patterns 11/8/2018 14 14

Programming Design Patterns LightOn Command public class LightOnCommand implements Command { Light light; public LightOnCommand(Light lightIn) { light = LightIn; } // execute calls specific methods on // the receiving object public void execute() { light.on(); public void undo() { light.off(); Programming Design Patterns 11/8/2018 15 15

Programming Design Patterns History of Undo? How can you implement a history of Undo operations? Homework? Use a stack to keep track of the executed commands Programming Design Patterns 11/8/2018 16 16

Programming Design Patterns Execute a series of steps? public void execute() { stereo.on(); stereo.setCD(); stereo.setVolume(); } Programming Design Patterns 11/8/2018 17 17

Programming Design Patterns Error handling What if a command is not loaded in a slot? Write code for slots in which an appropriate object may not be loaded (or perhaps the command object for that slot is null) CLASSWORK Programming Design Patterns 11/8/2018 18 18

Programming Design Patterns Null Command (2) public void onButtonWasPressed(int slot) { if (onCommands[slot] != null) { onCommands[slot].execute(); } With this implementation approach client is forced to handle cases with an “if” loop Programming Design Patterns 11/8/2018 19 19

Programming Design Patterns Null Command (3) public class NoCommand implements Command { public void execute() { } Load the Null Command onto empty slots NoCommand: example of a null object Removes the responsibility from the client to handle null cases Null Object is sometimes listed as a design pattern Programming Design Patterns 11/8/2018 20 20

Programming Design Patterns Homework Page 228 in the book queuing requests This idea is used in SETI@Home and Java Spaces Programming Design Patterns 11/8/2018 21 21

Programming Design Patterns Problem Statement Suppose you have a set of worker computers that donate free CPU cycles to solve problems A problem can be broken into various parts, each of which can be independently solved by any of the free worker-computers. The various parts of the problem can be dropped off as objects into a shared space (think of it as a queue). Design code for this “Shared-Problem-Space” scenario. (similar to Java Spaces) Programming Design Patterns 11/8/2018 22 22

Programming Design Patterns Command Pattern Encapsulate a request as an object letting you parameterize clients with different requests Homework Queue or log requests, Support undoable operations. Programming Design Patterns 11/8/2018 23 23

Programming Design Patterns Command Pattern: features Can issue requests to objects without knowing anything about the operation being requested or the receiver of the request. Decouples the object that invokes the operation from the one that knows how to perform it. The action class contains an execute() method that simply calls the action on the receiver. All clients of Command objects treat each object as a "black box" by simply invoking the object's execute() method whenever the client requires the object's "service". Programming Design Patterns 11/8/2018 24 24

Programming Design Patterns Command Pattern Features (2) Sequences of Command objects can be assembled into composite (or macro) commands. Programming Design Patterns 11/8/2018 25 25

Programming Design Patterns Check List for Command Pattern Define a Command interface public void execute(). Create one or more derived classes that encapsulate some subset of the following: a "receiver" object, the method to invoke, the arguments to pass. Instantiate a Command object for each deferred execution request. Pass the Command object from the creator (sender) to the invoker (receiver). The invoker decides when to execute(). Programming Design Patterns 11/8/2018 26 26

Programming Design Patterns 1. Document Editor Menus In a document editor, there are often lots of pull-down menus, containing lots of menu items. At any given time during the editing process, some of these are enabled, and some disabled. For example, an "Edit" pull-down menu may have menu items for "Undo Clear", "Redo Paste", "Cut", "Copy", "Paste", and "Paste as Hyperlink". Design a simple menu- driven editor that allows you to choose different pull-down menus, and shows options for the ones that are enabled and ones that are disabled. Specific methods should be called whenever a menu option is selected. However, the method implementation should just print a statement saying that it was called. Programming Design Patterns 11/8/2018 27 27

Programming Design Patterns 2. University Admissions Office The admissions office of a university keeps online documentation in a single format. It gets updated frequently and the single format helps maintenance (make changes in only one place). However, the new requirement is that the users should be able to download information in different formats such as HTML, PDF, and Word. Assume that as a Computer Science major you are aware of open source packages that convert the information to these formats. Programming Design Patterns 11/8/2018 28 28