Advertisement
Guest User

Remote Control

a guest
Oct 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.95 KB | None | 0 0
  1. // the command interface
  2. public interface Command {
  3.    
  4.     public void execute();
  5.  
  6.     public void undo();
  7. }
  8.  
  9. import java.util.Stack;
  10.  
  11. // invoker
  12. public class DeviceButton {
  13.     Stack<Command> stack = new Stack<Command>();
  14.     public void execute(Command cmd){
  15.        
  16.         System.out.println("Device button is pressed!");
  17.         stack.add(cmd);
  18.         cmd.execute();
  19.     }
  20.  
  21.     public void undo(Command cmd) {
  22.         System.out.println("Undoing last command!");
  23.         stack.add(cmd);
  24.         cmd.undo();
  25.  
  26.     }
  27. }
  28. public interface ElectronicDevice {
  29.    
  30.     public void on();
  31.    
  32.     public void off();
  33.    
  34.     public void volumeUp();
  35.    
  36.     public void volumeDown();
  37.    
  38. }
  39. // the client
  40. public class PlayWithRemote{
  41.    
  42.     public static void main(String[] args){
  43.         DeviceButton button = new DeviceButton();
  44.         //TV commands
  45.         // create command receiver
  46.         ElectronicDevice newTV = RemoteControl.GetTelevision();
  47.         // create a concrete command
  48.         TurnOn onTVCommand = new TurnOn(newTV);
  49.         TurnOff offTVCommand = new TurnOff(newTV);
  50.         VolumeUp tvVolumeUp = new VolumeUp(newTV);
  51.         VolumeDown tvVolumeDown = new VolumeDown(newTV);
  52.         // create invoker
  53.  
  54.        
  55.         // invoke command
  56.  
  57.        
  58.         // create another concrete command        
  59.  
  60.  
  61.         // invoke another concrete command
  62.         button.execute(onTVCommand);
  63.         button.undo(onTVCommand);
  64.         button.execute(offTVCommand);
  65.         button.undo(offTVCommand);
  66.  
  67.         //Stereo Commands
  68.         ElectronicDevice newStereo = RemoteControl.GetStereo();
  69.         //concrete commands for stereo
  70.         TurnOn onStereoCommand = new TurnOn(newStereo);
  71.         TurnOff offStereoCommand = new TurnOff(newStereo);
  72.         VolumeUp stereoVolumeUp = new VolumeUp(newStereo);
  73.         VolumeDown stereoVolumeDown = new VolumeDown(newStereo);
  74.         // create invoker
  75.         // invoke commands
  76.         button.execute(onStereoCommand);
  77.         button.undo(onStereoCommand);
  78.         button.execute(stereoVolumeUp);
  79.         button.undo(stereoVolumeUp);
  80.         button.execute(stereoVolumeDown);
  81.         button.undo(stereoVolumeDown);
  82.         button.execute(offStereoCommand);
  83.         button.undo(offStereoCommand);
  84.     }
  85. }
  86.  
  87. public class RemoteControl {
  88.  
  89.     // return a command receiver
  90.     public static ElectronicDevice GetTelevision(){
  91.        
  92.         return new Television();
  93.     }
  94.  
  95.     public static ElectronicDevice GetStereo() {
  96.  
  97.         return new Stereo();
  98.     }
  99. }
  100. // a comand receiver
  101. public class Stereo implements ElectronicDevice {
  102.  
  103.     private int volume = 0;
  104.  
  105.     public void on() {
  106.  
  107.         System.out.println("Stereo is on");
  108.  
  109.     }
  110.  
  111.     public void off() {
  112.  
  113.         System.out.println("Stereo is off");
  114.  
  115.     }
  116.  
  117.     public void volumeUp() {
  118.  
  119.         volume++;
  120.  
  121.         System.out.println("Stereo Volume is at: " + volume);
  122.  
  123.     }
  124.  
  125.     public void volumeDown() {
  126.  
  127.         volume--;
  128.  
  129.         System.out.println("Stereo is at: " + volume);
  130.  
  131.     }
  132.  
  133. }
  134. // a comand receiver
  135. public class Television implements ElectronicDevice {
  136.  
  137.     private int volume = 0;
  138.    
  139.     public void on() {
  140.        
  141.         System.out.println("TV is on");
  142.        
  143.     }
  144.  
  145.     public void off() {
  146.        
  147.         System.out.println("TV is off");
  148.        
  149.     }
  150.  
  151.     public void volumeUp() {
  152.        
  153.         volume++;
  154.        
  155.         System.out.println("TV Volume is at: " + volume);
  156.        
  157.     }
  158.  
  159.     public void volumeDown() {
  160.        
  161.         volume--;
  162.        
  163.         System.out.println("TV Volume is at: " + volume);
  164.        
  165.     }
  166.    
  167. }
  168. // a concrete command
  169. public class TurnOff implements Command {
  170.  
  171.     ElectronicDevice myDevice;
  172.  
  173.     public TurnOff(ElectronicDevice device){
  174.  
  175.         myDevice = device;
  176.     }
  177.  
  178.     public void execute() {
  179.  
  180.         myDevice.off();
  181.     }
  182.  
  183.     @Override
  184.     public void undo() {
  185.        myDevice.on();
  186.     }
  187. }
  188. // a concrete command
  189. public class TurnOn implements Command {
  190.  
  191.     ElectronicDevice myDevice;
  192.    
  193.     public TurnOn(ElectronicDevice device){
  194.         // register receiver
  195.         myDevice = device;
  196.     }
  197.    
  198.     public void execute() {
  199.  
  200.         myDevice.on();
  201.     }
  202.     @Override
  203.     public void undo() {
  204.         myDevice.off();
  205.     }
  206. }
  207.  
  208. public class VolumeDown implements Command {
  209.  
  210.     ElectronicDevice myDevice;
  211.  
  212.     public VolumeDown(ElectronicDevice device){
  213.  
  214.         myDevice = device;
  215.     }
  216.  
  217.     public void execute() {
  218.  
  219.         myDevice.volumeDown();
  220.     } @Override
  221.     public void undo() {
  222.         myDevice.volumeUp();
  223.     }
  224.  
  225.  
  226. }
  227.  
  228. public class VolumeUp implements Command {
  229.  
  230.     ElectronicDevice myDevice;
  231.  
  232.     public VolumeUp(ElectronicDevice device){
  233.  
  234.         myDevice = device;
  235.     }
  236.  
  237.     public void execute() {
  238.  
  239.         myDevice.volumeUp();
  240.     }
  241.     @Override
  242.     public void undo() {
  243.         myDevice.volumeDown();
  244.     }
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement