File tree 9 files changed +97
-1
lines changed
src/main/java/com/example/command/v1 9 files changed +97
-1
lines changed Original file line number Diff line number Diff line change @@ -10,4 +10,5 @@ Behavioural patterns are all about the interaction or communication between the
10
10
| State Pattern | state-pattern |
11
11
| Iterator Pattern | iterator-pattern |
12
12
| Strategy Pattern | strategy-pattern |
13
- | Template Method Pattern | template-method-pattern |
13
+ | Template Method Pattern | template-method-pattern |
14
+ | Command Pattern | command-pattern |
Original file line number Diff line number Diff line change
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.**
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package com .example .command .v1 ;
2
+
3
+ public class CustomerServiceReceiver {
4
+
5
+ public void addCustomer () {
6
+ System .out .println ("Add customer" );
7
+ }
8
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package com .example .command .v1 .framework ;
2
+
3
+ public interface Command {
4
+
5
+ void execute ();
6
+ }
Original file line number Diff line number Diff line change 15
15
<module >iterator-pattern</module >
16
16
<module >strategy-pattern</module >
17
17
<module >template-method-pattern</module >
18
+ <module >command-pattern</module >
18
19
</modules >
19
20
20
21
<properties >
You can’t perform that action at this time.
0 commit comments