CS 350 – Software Design Command Object Remote Control Object.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
The Singleton Pattern II Recursive Linked Structures.
FEN KbP: Seminar 2/JML-Intro1 JML Introduktion Specifikation af en Personklasse.
Command Explained. Intent Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests,
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.
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
March Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
Spring 2010ACS-3913 Ron McFadyen1 Command The complex remote control (many pairs of buttons; null command) Undo … pages 216+ Macro … pages 224+
Feb Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface the client expects. An adapter lets classes work.
MAHARISHI INTERNATIONAL UNIVERSITY M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature.
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.
Methods & Activation Record. Recap: what’s a method?
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006.
CS 210 Introduction to Design Patterns September 28 th, 2006.
CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming Progamz pls. Importance VERY IMPORTANT.
CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
FEN UCN T&B - PBA/CodeContract- Intro 1 Code Contract Introduction Specification of a Person Class.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Refactoring An Automated Tool for the Tiger Language Leslie A Hensley
CS 210 Introduction to Design Patterns September 26 th, 2006.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 210 Review Session October 5 th, Head First Design Patterns Chapter 4 Factory Pattern.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
State Design Pattern. Behavioral Pattern Allows object to alter its behavior when internal state changes Uses Polymorphism to define different behaviors.
1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things.
Interfaces and Polymorphism CS 162 (Summer 2009).
Converting string to integer class StringToInt { public static void main (String arg[]) { // Assume arg[0] is the input string int result = 0, pos; int.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Command Pattern Encapsulation Invocation. One size fits all.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Staples are our staple Building upon our solution.
GC211 Data structure Lecture 3 Sara Alhajjam.
using System; namespace Demo01 { class Program
Programming Design Patterns
Command Pattern 1.
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
Functions Used to write code only once Can use parameters.
Computing Adjusted Quiz Total Score
null, true, and false are also reserved.
An Introduction to Java – Part I, language basics
Interfaces and Constructors
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Assignment 7 User Defined Classes Part 2
Code Animation Examples
Example with Static Variable
Recursive GCD Demo public class Euclid {
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS 180 Assignment 6 Arrays.
Composite describes a group of objects that is treated the same way as a single instance of the same type of object. composition of one-or-more similar.
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Topics OOP Review Inheritance Review Abstract Classes
State Design Pattern.
Presentation transcript:

CS 350 – Software Design Command Object Remote Control Object

CS 350 – Software Design Command Object Simple Command Object Implementation public interface Command { public void execute(); public void execute();} public class LightOnCommand implements Command { Light light; Light light; public LightOnCommand(Light light) { public LightOnCommand(Light light) { this.light = light; this.light = light;} public void execute () { public void execute () { light.on(); light.on(); }}

CS 350 – Software Design Command Object public class SimpleRemoteControl { Command slot; Command slot; public SimpleRemoteControl { public SimpleRemoteControl { public void setCommand(Command command) { public void setCommand(Command command) { slot = command; slot = command; } public void buttonWasPressed() { public void buttonWasPressed() { slot.execute(); slot.execute(); }}

CS 350 – Software Design Command Object public class RemoteControlTest { public static void main(String[] args) { public static void main(String[] args) { SimpleRemoteControl remote = new SimpleRemoteControl(); SimpleRemoteControl remote = new SimpleRemoteControl(); Light light = new Light(); Light light = new Light(); LightOnCommand lightOn = new LightOnCommand(light); LightOnCommand lightOn = new LightOnCommand(light); remote.setCommand(lightOn); remote.setCommand(lightOn); remote.buttonWasPressed(); remote.buttonWasPressed(); }}

CS 350 – Software Design Command Object SHOW COMMAND PATTERN

CS 350 – Software Design Command Object IMPLEMENTING THE REMOTE CONTROL public class RemoteControl { Command[] on Commands; Command[] on Commands; Command[] offCommands; Command[] offCommands; public RemoteControl() { public RemoteControl() { onCommands = new Command[7]; onCommands = new Command[7]; offCommands = new Command[7]; offCommands = new Command[7]; Command noCommand = new NoCommand(); Command noCommand = new NoCommand(); for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) { onCommands[i] = noCommand; onCommands[i] = noCommand; offCommands[i] = noCommand; offCommands[i] = noCommand; }} public void setCommand(int slot, Command onCommand, Command offCommand) { onCommand[slot] = onCommand; onCommand[slot] = onCommand; offCommand[slot] = offCommand; offCommand[slot] = offCommand;}

CS 350 – Software Design Command Object IMPLEMENTING THE REMOTE CONTROL public void onButtonWasPushed(int slot) { onCommands(slot].execute(); onCommands(slot].execute();} public void offButtonWasPushed(int slot) { offCommands(slot].execute(); offCommands(slot].execute();} public String toString() { StringBuffer stringBuff = newStringBuffer(); StringBuffer stringBuff = newStringBuffer(); strongBuff.append(“\n Remote Control \n”); strongBuff.append(“\n Remote Control \n”); for (int i = 0; i < onCommands.length; i++) { for (int i = 0; i < onCommands.length; i++) { stringBuff.append(“[slot “ + i + “] “ + onCommands[i].getClass().getName() + stringBuff.append(“[slot “ + i + “] “ + onCommands[i].getClass().getName() + “ “ + offcommands[i].getClass().getName() + “\n”; “ “ + offcommands[i].getClass().getName() + “\n”; } return stringBuff.toString(); } return stringBuff.toString(); }}

CS 350 – Software Design Command Object IMPLEMENTING THE COMMANDS public class LightOffCommand implements Command { Light light; Light light; public LightOffCommand(Light light) { public LightOffCommand(Light light) { this.light = light; this.light = light; } public void execute() { public void execute() { light.off(); light.off(); }}

CS 350 – Software Design Command Object IMPLEMENTING THE COMMANDS public class StereoOnWithCDCommand implements Command { Stereo stereo; Stereo stereo; public StereoOnWithCDCommand(Stereo stereo) { public StereoOnWithCDCommand(Stereo stereo) { this.stereo = stereo; this.stereo = stereo; public void execute() { public void execute() { stereo.on(); stereo.on(); stereo.setCD(); stereo.setCD(); stereo.setVolume(11); stereo.setVolume(11); }}

CS 350 – Software Design Command Object REMOTE CONTROL public class RemoteLoader { public static void main(String[] args) { public static void main(String[] args) { RemoteControl remoteControl = new RemoteControl(); RemoteControl remoteControl = new RemoteControl(); Light livingRoom = newLight(“Living Room”); Light livingRoom = newLight(“Living Room”); Light kitchenLight = new Light(“Kitchen”); Light kitchenLight = new Light(“Kitchen”); CeilingFan ceilingFan = new CeilingFan(“Living Room”); CeilingFan ceilingFan = new CeilingFan(“Living Room”); GarageDoor garageDoor = new GarageDoor(“”); GarageDoor garageDoor = new GarageDoor(“”); Stereo stereo = new Stereo (“Living Room”); Stereo stereo = new Stereo (“Living Room”); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCoammd livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOffCoammd livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand(kitchenRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand(kitchenRoomLight); LightOffCommand kitchenLightOn = new LightOffCommand(kitchenRoomLight); LightOffCommand kitchenLightOn = new LightOffCommand(kitchenRoomLight);

CS 350 – Software Design Command Object REMOTE CONTROL CeilingFanOnCommand ceilingFanOn = new ceilingFanOnCommand(ceilingFan); CeilingFanOnCommand ceilingFanOn = new ceilingFanOnCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new ceilingFanOffCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new ceilingFanOffCommand(ceilingFan); GarageDoorUpCommand garageDoorUp = new garageDoorUpCommand(garageDoor); GarageDoorUpCommand garageDoorUp = new garageDoorUpCommand(garageDoor); GarageDoorDownCommand garageDoorDown = new garageDoorDownCommand(garageDoor); GarageDoorDownCommand garageDoorDown = new garageDoorDownCommand(garageDoor); StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo); StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo); StereoOffCommand stereoOff = new StereoOffCommand(stereo); StereoOffCommand stereoOff = new StereoOffCommand(stereo); remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff); remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff); remoteControl.setCommand(3, stereoOnWithCD, stereoOff); remoteControl.setCommand(3, stereoOnWithCD, stereoOff); System.out.println(remoteControl); System.out.println(remoteControl);

CS 350 – Software Design Command Object REMOTE CONTROL remoteControl.onButtonWasPushed(0); remoteControl.onButtonWasPushed(0); remoteControl.offButtonWasPushed(0); remoteControl.offButtonWasPushed(0); remoteControl.onButtonWasPushed(1); remoteControl.onButtonWasPushed(1); remoteControl.offButtonWasPushed(1); remoteControl.offButtonWasPushed(1); remoteControl.onButtonWasPushed(2); remoteControl.onButtonWasPushed(2); remoteControl.offButtonWasPushed(2); remoteControl.offButtonWasPushed(2); remoteControl.onButtonWasPushed(3); remoteControl.onButtonWasPushed(3); remoteControl.offButtonWasPushed(3); remoteControl.offButtonWasPushed(3);

CS 350 – Software Design Command Object REMOTE CONTROL – SHORT CUTS We do not want to code as follows: public void onButtonWasPushed(int slot) { if (onCommands[slot[ != null) { if (onCommands[slot[ != null) { onCommands[slot].execute(); onCommands[slot].execute(); }} Instead we code: public class NoCommand implements Command ( public void execute() { } public void execute() { }} In the remote control constructor we code: Command noCommand = new NoCommand(): for (int i = 0; i < 7; i++) onCommands[i] = noCommand; onCommands[i] = noCommand; offCommands[i] = noCommand; offCommands[i] = noCommand;}

CS 350 – Software Design Command Object ADDING UNDO public interface Command { public void execute(); public void execute(); public void undo(); public void undo();} Easy for some objects public class LightOnCommand implements Command { Light light; Light light; public LightOnCOmmand(Light light) { public LightOnCOmmand(Light light) { this.light = light; this.light = light; } public void execute() { public void execute() { light.on(); light.on(); } public void undo() { public void undo() { light.off(); light.off(); }}

CS 350 – Software Design Command Object ADDING UNDO public class LightOffCommand implements Command { Light light; Light light; public LightOffCommand(Light light) { public LightOffCommand(Light light) { this.light = light; this.light = light; } public void execute() { public void execute() { light.off(); light.off(); } public void undo() { public void undo() { light.on(); light.on(); }}

CS 350 – Software Design Command Object IMPLEMENTING THE REMOTE CONTROL WITH UNDO public class RemoteControl { Command[] on Commands; Command[] on Commands; Command[] offCommands; Command[] offCommands; Command[] undoCommands; Command[] undoCommands; public RemoteControl() { public RemoteControl() { onCommands = new Command[7]; onCommands = new Command[7]; offCommands = new Command[7]; offCommands = new Command[7]; Command noCommand = new NoCommand(); Command noCommand = new NoCommand(); for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) { onCommands[i] = noCommand; onCommands[i] = noCommand; offCommands[i] = noCommand; offCommands[i] = noCommand; } undoCommand = noCommand; } public void setCommand(int slot, Command onCommand, Command offCommand) { onCommand[slot] = onCommand; onCommand[slot] = onCommand; offCommand[slot] = offCommand; offCommand[slot] = offCommand;}

CS 350 – Software Design Command Object public void onButtonWasPushed(int slot) { onCommands(slot].execute(); onCommands(slot].execute(); undoCommand = onCommands[slot]; undoCommand = onCommands[slot];} public void offButtonWasPushed(int slot) { offCommands(slot].execute(); offCommands(slot].execute(); undoCommand = offCommands[slot]; undoCommand = offCommands[slot];} public void undoButtonwWasPushed() { undoCommand.undo(); undoCommand.undo();} public String toString() { //same}

CS 350 – Software Design Command Object public class CeilingFan { public static final int HIGH = 3; public static final int HIGH = 3; public static final int MEDIUM = 2; public static final int MEDIUM = 2; public static final int LOW= 1; public static final int LOW= 1; public static final int OFF= 0; public static final int OFF= 0; public CeilingFan(String location) { public CeilingFan(String location) { this.location = location; this.location = location; speed = OFF; speed = OFF; } public void high() { public void high() { speed = HIGH; speed = HIGH; //code to set fan to high //code to set fan to high} public void medium() { public void medium() { speed = MEDIUM; speed = MEDIUM; //code to set fan to medium //code to set fan to medium}

CS 350 – Software Design Command Object public void low() { public void low() { speed = LOW; speed = LOW; //code to set fan to low //code to set fan to low} public void off() { public void off() { speed = OFF; speed = OFF; //code to turn fan off //code to turn fan off} public int getSpeed() { return speed; return speed; }}

CS 350 – Software Design Command Object Adding Undo to the ceiling fan commands public class CeilingFanHighCommand implements Command { CeilingFan ceilingFan; CeilingFan ceilingFan; int prevSpeed; int prevSpeed; public CeilingFanHighCommand(CeilingFan ceilingFan) { public CeilingFanHighCommand(CeilingFan ceilingFan) { this.ceilingFan = ceilingFan; this.ceilingFan = ceilingFan;} public void execute() { public void execute() { prevSpeed = ceilingFan.getSpeed(); prevSpeed = ceilingFan.getSpeed(); ceilingFan.high(); ceilingFan.high();} public void undo() { if (prevSpeed == CeilingFan.HIGH) { if (prevSpeed == CeilingFan.HIGH) { celingFan.high(); celingFan.high(); } else if (prevSpeed = CeilingFan.MEDIUM) { } else if (prevSpeed = CeilingFan.MEDIUM) { ceilingFan.medium(); ceilingFan.medium(); } else if (prevSpeed = CeilingFan.LOW) { } else if (prevSpeed = CeilingFan.LOW) { ceilingFan.low(); ceilingFan.low(); } else if (prevSpeed = CeilingFan.OFF) { } else if (prevSpeed = CeilingFan.OFF) { ceilingFan.off();}} ceilingFan.off();}}

CS 350 – Software Design Command Object MACRO COMMAND public class MacroCommand implements Command { Command[] commands; Command[] commands; public MacroCommand(Command[] commands) { public MacroCommand(Command[] commands) { this.commands = commands; this.commands = commands;} public void execute() { public void execute() { for (int i = 0; i < commands.length; i++) { for (int i = 0; i < commands.length; i++) { commands[i].execute(); commands[i].execute(); } }}

CS 350 – Software Design Command Object USING A MACRO COMMAND Light light = new Light(“Living Room”); TV tv = new TV(“Living Room”); Stereo stereo = new Stereo(“Living Room”); Hottub hottub = new Hottub(); LightOnCommand lightOn = new LightOnCommand(light); StereoOnCommand stereoOn = new StereoOnCommand(stereo); TVOnCommand tvOn = newTVOnCommand(tv); HotTubOnCommand hottubOn = newHottubOnCommand(hottub); LightOffCommand lightOff = new LightOffCommand(light); StereoOffCommand stereoOff = new StereoOffCommand(stereo); TVOffCommand tvOff = newTVOffCommand(tv); HotTubOffCommand hottubOff = newHottubOffCommand(hottub); Command[] partyOn = (lightOn, stereoOn, tvOn, hottubOn); Command[] partyOff = (lightOff, stereoOff, tvOff, hottubOff); MacroCommand partyOnMacro = newMacroCommand(partyOn); MacroCommand partyOffMacro = newMacroCommand(partyOff); Remotecontrol.setCommand(0, partyOnMacro, partyOffMacro);