Skip to content

Commit d2afa80

Browse files
committed
Command design pattern - AFTER CODE - Light switch project
Loose coupling and commands can be added dynamically with almost zero code changes in client file
1 parent 2116da6 commit d2afa80

3 files changed

Lines changed: 86 additions & 3 deletions

File tree

pattern/src/com/premaseem/Client.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,31 @@
1010
public 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
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.premaseem;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
/**
9+
* The Command functional interface.
10+
*/
11+
@FunctionalInterface
12+
public interface Command {
13+
public void execute();
14+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.premaseem;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
import java.util.stream.Collectors;
12+
13+
public final class CommandFactory {
14+
private final Map<String, Command> commands;
15+
16+
private CommandFactory() {
17+
commands = new HashMap<>();
18+
}
19+
20+
public void addCommand(final String name, final Command command) {
21+
commands.put(name, command);
22+
}
23+
24+
public void executeCommand(String name) {
25+
if (commands.containsKey(name)) {
26+
commands.get(name).execute();
27+
}else{
28+
System.out.println(name+ " Command not found");
29+
}
30+
}
31+
32+
public void listCommands() {
33+
System.out.println("Available commands: " + commands.keySet().stream().collect(Collectors.joining(", ")));
34+
}
35+
36+
/* Factory pattern */
37+
public static CommandFactory init() {
38+
final CommandFactory cf = new CommandFactory();
39+
40+
// commands are added here using lambdas.
41+
// It is also possible to dynamically add commands without editing the code.
42+
cf.addCommand("LightOn", () -> System.out.println("Light turned on"));
43+
cf.addCommand("LightOff", () -> System.out.println("Light turned off"));
44+
45+
return cf;
46+
}
47+
}

0 commit comments

Comments
 (0)