Skip to content

Commit b741379

Browse files
committed
add Command pattern v1 implementation
1 parent 51ef89c commit b741379

File tree

9 files changed

+97
-1
lines changed

9 files changed

+97
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ Behavioural patterns are all about the interaction or communication between the
1010
| State Pattern | state-pattern |
1111
| Iterator Pattern | iterator-pattern |
1212
| Strategy Pattern | strategy-pattern |
13-
| Template Method Pattern | template-method-pattern |
13+
| Template Method Pattern | template-method-pattern |
14+
| Command Pattern | command-pattern |

command-pattern/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Command Design Pattern
2+
3+
**Command pattern is used to implement loose coupling in a request-response model. It is used to decouple the sender from the receiver.**

command-pattern/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-behavioral-design-patterns</artifactId>
7+
<groupId>com.example</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>command-pattern</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>16</maven.compiler.source>
16+
<maven.compiler.target>16</maven.compiler.target>
17+
</properties>
18+
19+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example.command.v1;
2+
3+
import com.example.command.v1.framework.Command;
4+
5+
public class AddCustomerConcreteCommand implements Command {
6+
7+
private CustomerServiceReceiver customerServiceReceiver;
8+
9+
public AddCustomerConcreteCommand(CustomerServiceReceiver customerServiceReceiver) {
10+
this.customerServiceReceiver = customerServiceReceiver;
11+
}
12+
13+
@Override
14+
public void execute() {
15+
// delegating the work to add customer method of customer service receiver
16+
customerServiceReceiver.addCustomer();
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.command.v1;
2+
3+
import com.example.command.v1.framework.ButtonInvoker;
4+
import com.example.command.v1.framework.Command;
5+
6+
public class Application {
7+
8+
public static void main(String[] args) {
9+
10+
CustomerServiceReceiver customerServiceReceiver = new CustomerServiceReceiver();
11+
Command addCustomerConcreteCommand = new AddCustomerConcreteCommand(customerServiceReceiver);
12+
ButtonInvoker buttonInvoker = new ButtonInvoker(addCustomerConcreteCommand);
13+
14+
buttonInvoker.click();
15+
}
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.command.v1;
2+
3+
public class CustomerServiceReceiver {
4+
5+
public void addCustomer() {
6+
System.out.println("Add customer");
7+
}
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.command.v1.framework;
2+
3+
public class ButtonInvoker {
4+
5+
private String label;
6+
private Command command;
7+
8+
public ButtonInvoker(Command command) {
9+
this.command = command;
10+
}
11+
12+
public void click() {
13+
// delegating the work to command object
14+
command.execute();
15+
}
16+
17+
public String getLabel() {
18+
return label;
19+
}
20+
21+
public void setLabel(String label) {
22+
this.label = label;
23+
}
24+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.command.v1.framework;
2+
3+
public interface Command {
4+
5+
void execute();
6+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<module>iterator-pattern</module>
1616
<module>strategy-pattern</module>
1717
<module>template-method-pattern</module>
18+
<module>command-pattern</module>
1819
</modules>
1920

2021
<properties>

0 commit comments

Comments
 (0)