Advertisement
otisphat80

Command Pattern

Sep 1st, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.61 KB | None | 0 0
  1. #ifndef COMMAND_H
  2. #define COMMAND_H
  3.  
  4. class Command
  5. {
  6. public:
  7.     Command() { }
  8.     virtual void execute() { }
  9.    
  10. };
  11.  
  12. #endif // COMMAND_H
  13. #ifndef CEILINGFANOFFCOMMAND_H
  14. #define CEILINGFANOFFCOMMAND_H
  15.  
  16. #include "command.h" // Base class: Command
  17. #include "ceiling_fan.h"
  18.  
  19. class CeilingFanOffCommand : public Command
  20. {
  21. public:
  22.     CeilingFanOffCommand(CeilingFan *ceilingFan) { m_ceilingFan = ceilingFan; }
  23.     void execute() { m_ceilingFan->off(); }
  24.    
  25. private:
  26.     CeilingFan *m_ceilingFan = nullptr;
  27. };
  28.  
  29. #endif // CEILINGFANOFFCOMMAND_H
  30. #ifndef CEILINGFANONCOMMAND_H
  31. #define CEILINGFANONCOMMAND_H
  32.  
  33. #include "command.h"
  34. #include "ceiling_fan.h"
  35.  
  36. class CeilingFanOnCommand : public Command
  37. {
  38. public:
  39.     CeilingFanOnCommand(CeilingFan *ceilingFan) {  m_ceilingFan = ceilingFan; }
  40.     void execute() { m_ceilingFan->high(); }
  41.    
  42. private:
  43.     CeilingFan *m_ceilingFan = nullptr;
  44.    
  45. };
  46.  
  47. #endif // CEILINGFANONCOMMAND_H
  48. #ifndef GARAGEDOOR_H
  49. #define GARAGEDOOR_H
  50.  
  51. #include <iostream>
  52.  
  53. class GarageDoor
  54. {
  55. public:
  56.     GarageDoor() { }
  57.     void up()       { std::cout << "Garage Door is Open" << std::endl; }
  58.     void down()     { std::cout << "Garage Door is Closed" << std::endl; }
  59.     void stop()     { std::cout << "Garage Door is Stopped" << std::endl; }
  60.     void lightOn()  { std::cout << "Garage light is on" << std::endl; }
  61.     void lightOff() { std::cout << "Garage light is off" << std::endl; }   
  62. };
  63.  
  64. #endif // GARAGEDOOR_H
  65. #ifndef GARAGEDOOROPENCOMMAND_H
  66. #define GARAGEDOOROPENCOMMAND_H
  67.  
  68. #include "command.h" // Base class: Command
  69. #include "garage_door.h"
  70.  
  71. class GarageDoorOpenCommand : public Command
  72. {
  73.     public:
  74.         GarageDoorOpenCommand(GarageDoor garageDoor ) { m_garagedoor = garageDoor; }
  75.         void execute() { m_garagedoor.up(); }
  76.     private:
  77.         GarageDoor m_garagedoor;  //pointer??
  78.  
  79. };
  80.  
  81. #endif // GARAGEDOOROPENCOMMAND_H
  82. #ifndef HOTTUBOFFCOMMAND_h
  83. #define HOTTUBOFFCOMMAND_h
  84.  
  85. #include "command.h" // Base class: Command
  86. #include "hottube.h"
  87.  
  88. class HotTubOffCommand : public Command
  89. {
  90. public:
  91.     HotTubOffCommand(Hottube *hottub) { m_hottub = hottub; }
  92.     void execute() { m_hottub->cool(); m_hottub->off(); }
  93.  
  94. private:
  95.     Hottube *m_hottub = nullptr;
  96.    
  97. };
  98.  
  99. #endif // HOTTUBOFFCOMMAND_h
  100. #ifndef HOTTUBONCOMMAND_H
  101. #define HOTTUBONCOMMAND_H
  102.  
  103. #include "command.h" // Base class: Command
  104. #include "hottube.h"
  105.  
  106. class HotTubOnCommand : public Command
  107. {
  108. public:
  109.     HotTubOnCommand(Hottube *hottub) { m_hottub = hottub; }
  110.     void execute() { m_hottub->on(); m_hottub->heat(); m_hottub->bubblesOn(); }
  111.    
  112. private:
  113.     Hottube *m_hottub = nullptr;
  114. };
  115.  
  116. #endif // HOTTUBONCOMMAND_H
  117. #ifndef HOTTUBE_H
  118. #define HOTTUBE_H
  119.  
  120. #include <iostream>
  121. #include <string>
  122.  
  123. class Hottube
  124. {
  125. public:
  126.     Hottube( ) { }
  127.     void on() { m_on = true; }
  128.     void off() { m_on = false; }
  129.     void bubblesOn() { if (m_on) { std::cout << "Hottub is bubbling!\n"; }}
  130.     void bubblesOff() { if (m_on) { std::cout << "Hottub is not bubbling!\n"; }}
  131.     void jetsOn() { if (m_on) { std::cout << "Hottub jets are on!\n"; }}
  132.     void jetsOff() { if (m_on) { std::cout << "Hottub jets are off!\n"; }}
  133.     void setTemperature(int temperature) { m_temperature = temperature; }
  134.     void heat() { m_temperature = 105; std::cout << "Hottub is heating to a steaming 105 degrees\n"; }
  135.     void cool() { m_temperature = 98; std::cout << "Hottub is cooling to 98 degrees\n"; }
  136.    
  137. private:
  138.     bool m_on;
  139.     int m_temperature;
  140. };
  141. #endif //HOTTUBE_H
  142. #ifndef LIGHT_H
  143. #define LIGHT_H
  144.  
  145. #include "stdio.h"
  146.  
  147. class Light
  148. {
  149. public:
  150.     Light() { }
  151.     void on()   { printf("%s\n", "Light is on!"); }
  152.     void off()  { printf("%s\n", "Light is off!"); }
  153. };
  154.  
  155. #endif // LIGHT_H
  156. #ifndef LIGHTOFFCOMMAND_H
  157. #define LIGHTOFFCOMMAND_H
  158.  
  159. #include "command.h" // Base class: Command
  160. #include "light.h"
  161.  
  162. class LightOffCommand : public Command
  163. {
  164. public:
  165.     LightOffCommand(Light light) { m_light = light; }
  166.     void execute() { m_light.off(); }
  167.  
  168. private:
  169.     Light m_light; //pointer ??
  170.    
  171. };
  172.  
  173. #endif // LIGHTOFFCOMMAND_H
  174. #ifndef NOCOMMAND_H
  175. #define NOCOMMAND_H
  176.  
  177. #include "command.h" // Base class: Command
  178.  
  179. class NoCommand : public Command
  180. {
  181. public:
  182.     NoCommand() { }
  183.     void execute() { }
  184. };
  185.  
  186. #endif // NOCOMMAND_H
  187. #ifndef STEREO_H
  188. #define STEREO_H
  189.  
  190. #include <string>
  191. #include <iostream>
  192.  
  193. class Stereo
  194. {
  195. public:
  196.     Stereo(std::string location) { m_location = location; }
  197.     void on() { std::cout << m_location << " stereo is on\n"; }
  198.     void off() { std::cout << m_location << " stereo is off\n"; }
  199.     void setCD() { std::cout << m_location << " stereo is set for CD input\n"; }
  200.     void setDVD() { std::cout << m_location << " stereo is set for DVD input\n"; }
  201.     void setRadio() { std::cout << m_location << " stereo is set for Radio\n"; }
  202.     void setVolume(int volume) { std::cout << m_location << " Stereo volume set to " + volume; }
  203.    
  204. private:
  205.     std::string m_location;
  206. };
  207.  
  208. #endif // STEREO_H
  209. #ifndef STEREOOFFCOMMAND_H
  210. #define STEREOOFFCOMMAND_H
  211.  
  212. #include "command.h" // Base class: Command
  213. #include "stereo.h"
  214.  
  215.  
  216. class StereoOffCommand : public Command
  217. {
  218. public:
  219.     StereoOffCommand(Stereo *stereo) { m_stereo = stereo; }
  220.     virtual void execute() { m_stereo->off(); }
  221.  
  222.  
  223. private:
  224.     Stereo *m_stereo = nullptr;
  225.    
  226. };
  227.  
  228. #endif // STEREOOFF
  229. #ifndef STEREOONWITHCDCOMMAND_H
  230. #define STEREOONWITHCDCOMMAND_H
  231.  
  232. #include "command.h" // Base class: Command
  233. #include "stereo.h"
  234.  
  235. class StereoOnWithCdCommand : public Command
  236. {
  237. public:
  238.     StereoOnWithCdCommand(Stereo *stereo) { m_stereo = stereo; }
  239.     void execute() { m_stereo->on(); m_stereo->setCD(); m_stereo->setVolume(11); }
  240.  
  241.  
  242. private:
  243.     Stereo *m_stereo = nullptr;
  244.    
  245. };
  246.  
  247. #endif
  248. #ifndef TV_H
  249. #define TV_H
  250.  
  251. #include <string>
  252. #include <iostream>
  253.  
  254. class TV
  255. {
  256. public:
  257.     TV(std::string location) { m_location = location; }
  258.     void on() { std::cout << "TV is on\n"; }
  259.     void off() { std::cout << "TV is off\n"; }
  260.     void setInputChannel() { m_channel = 3; std::cout << "Channel is set for VCR\n"; }
  261.    
  262. private:
  263.     std::string m_location;
  264.     int m_channel;
  265.  
  266. };
  267.  
  268. #endif // TV_H
  269. #ifndef REMOTECONTROL_H
  270. #define REMOTECONTROL_H
  271.  
  272. #include "command.h"
  273. #include "constants.h"
  274. #include "no_command.h"
  275.  
  276. class RemoteControl
  277. {
  278. public:
  279.     RemoteControl() {  
  280.         for(int index =0; index < NUMBER_OF_COMMANDS; index++) {
  281.             m_on_commands[index] = no_command;
  282.             m_off_commands[index] = no_command;
  283.         }
  284.     }
  285.    
  286.     void set_command(int slot, Command *on_command, Command *off_command) {
  287.         m_on_commands[slot] = on_command;
  288.                 m_off_commands[slot] = off_command;
  289.     }
  290.        
  291.     void button_was_pressed(int slot) {
  292.         m_on_commands[slot].execute();
  293.     }
  294.  
  295. private:
  296.     Command no_command;
  297.     Command m_on_commands[NUMBER_OF_COMMANDS];
  298.     Command m_off_commands[NUMBER_OF_COMMANDS];
  299. };
  300.  
  301. #endif // REMOTECONTROL_H
  302. #include "garage_door.h"
  303. #include "light.h"
  304. #include "garage_door_open_command.h"
  305. #include "light_off_command.h"
  306. #include "light_on_command.h"
  307. #include "remote_control.h"
  308. #include "ceiling_fan.h"
  309. #include "ceiling_fan_off_command.h"
  310. #include "ceiling_fan_on_command.h"
  311. #include "hottube.h"
  312. #include "hot_tub_on_command.h"
  313. #include "hot_tub_off_command.h"
  314. #include "living_room_light_off_command.h"
  315. #include "living_room_light_on_command.h"
  316. #include "stereo.h"
  317. #include "stereo_off_command.h"
  318. #include "stereo_on_with_cd_command.h"
  319.  
  320. int main(int argc, char **argv)
  321. {
  322.     RemoteControl remote_control;
  323.     GarageDoor g_door;
  324.     Light light_off;
  325.     Light light_on;
  326.     GarageDoorOpenCommand *g_door_cmd = new GarageDoorOpenCommand(g_door);
  327.     LightOnCommand *l_on = new LightOnCommand(light_on);
  328.     LightOffCommand *l_off = new LightOffCommand(light_off);
  329.     CeilingFan *ceilingFan = new CeilingFan("Living Room");
  330.     CeilingFanOffCommand *fan_off_cmd = new CeilingFanOffCommand(ceilingFan);
  331.     CeilingFanOnCommand *fan_on_cmd = new CeilingFanOnCommand(ceilingFan);
  332.     Hottube *hottub = new Hottube( );
  333.     HotTubOnCommand *tub_on_cmd = new HotTubOnCommand(hottub);
  334.     HotTubOffCommand *tub_off_cmd = new HotTubOffCommand(hottub);
  335.     LivingRoomLightOffCommand *room_light_off_cmd = new LivingRoomLightOffCommand(light_off);
  336.     LivingRoomLightOnCommand *room_on_cmd = new LivingRoomLightOnCommand(light_on);
  337.     Stereo *stereo = new Stereo("Back bed Room");
  338.     StereoOffCommand *stereo_off_cmd = new StereoOffCommand(stereo);
  339.     StereoOnWithCdCommand *stereo_on_cd = new StereoOnWithCdCommand(stereo);
  340.    
  341.     remote_control.set_command(0, stereo_on_cd, stereo_off_cmd);
  342.     remote_control.set_command(1, room_on_cmd, room_light_off_cmd);
  343.     remote_control.set_command(2, tub_on_cmd, tub_off_cmd);
  344.     remote_control.set_command(3, fan_on_cmd, fan_off_cmd);
  345.    
  346.     remote_control.button_was_pressed(0);
  347.     remote_control.button_was_pressed(1);
  348.     remote_control.button_was_pressed(2);
  349.     remote_control.button_was_pressed(3);
  350.    
  351.    
  352.     return 0;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement