1010public class Client {
1111 public static void main (String [] args ) {
1212 Scanner scanInput = new Scanner (System .in );
13- System .out .println ("Command Pattern Light switch project " );
14- System .out .println ("Master - what command you want to execute " );
15- String command = scanInput .next ();
13+ System .out .println ("Light switch Project using COMMAND DESIGN PATTERN \n " );
1614
15+ /** After implementing Command design pattern **/
16+
17+ // All available Commands will be created and init by command factory
18+ CommandFactory cf = CommandFactory .init ();
19+
20+ // Will list all commands which can be executed
21+ cf .listCommands ();
22+
23+ // Commands can be added and listed dynamically with ZERO code changes in client side
24+ System .out .println ("Master - what command you want to execute " );
25+ String command = scanInput .next ();
26+
27+ // Actual execution of command will happen
28+ cf .executeCommand (command );
29+
30+ /** Lessons Learnt - AFTER CODE
31+ # New commands can be added dynamically with ease
32+ # Client is loosely coupled and have no knowledge of encapsulated commands
33+ # Zero code changed needed at client when new commands are added
34+ */
35+
36+
37+ /** BEFORE CODE
1738 // Assume you will invoke a method to execute turn on method
1839 if (command.equalsIgnoreCase("LightOn")){
1940 System.out.println("Light turned on");
@@ -29,5 +50,6 @@ else if (command.equalsIgnoreCase("LightOff")){
2950 // Lots of if else ladder
3051 // Commands cannot be added Dynamically
3152 // Code is tightly coupled, no separation of responsibility
53+ */
3254 }
3355}
0 commit comments