diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 64112cd37b645a3948e9a462c336d1cd181e7fdd..f28b7ce225b9762ff859901f821377817d8d9423 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -20,6 +20,7 @@ + @@ -27,6 +28,7 @@ + diff --git a/.idea/encodings.xml b/.idea/encodings.xml index c156c3c1eb69215b7aff4cb6178a22c1f5c83871..b54dff9b404186f081940ae09bb10d03e5f11164 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -5,6 +5,8 @@ + + diff --git a/.idea/misc.xml b/.idea/misc.xml index 513980f7a19b944ae2709bde445ec3eacdd8e686..d093d144b979299946e94b04b6da5f953a065759 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -20,6 +20,7 @@ diff --git a/README.md b/README.md index bc1a05f8c4ec62e6e787b229c21f28c69507a0c2..233e91bbb78741e7f0db37629e280db35e24e271 100644 --- a/README.md +++ b/README.md @@ -325,7 +325,24 @@ * 实现者/接收者(Receiver)角色: 接收者,真正执行命令的对象。任何类都可能成为一个接收者,只要它能够实现命令要求实现的相应功能。 * 调用者/请求者(Invoker)角色: 要求命令对象执行请求,通常会持有命令对象,可以持有很多的命令对象。这个是客户端真正触发命令并要求命令执行相应操作的地方,也就是说相当于使用命令对象的入口。 +## 命令模式总结 +**1) 命令模式优点:** + +* 降低系统的耦合度。命令模式能将调用操作的对象与实现该操作的对象解耦。 +* 增加或删除命令非常方便。采用命令模式增加与删除命令不会影响其他类,它满足“开闭原则”,对扩展比较灵活。 +* 可以实现宏命令。命令模式可以与组合模式结合,将多个命令装配成一个组合命令,即宏命令。 + +**2) 命令模式缺点:** + +* 使用命令模式可能会导致某些系统有过多的具体命令类。 +* 系统结构更加复杂。 + +**3) 使用场景** + +* 系统需要将请求调用者和请求接收者解耦,使得调用者和接收者不直接交互。 +* 系统需要在不同的时间指定请求、将请求排队和执行请求。 +* 系统需要支持命令的撤销(Undo)操作和恢复(Redo)操作。 diff --git a/byv-command-patttern-20/.gitignore b/byv-command-patttern-20/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5ff6309b7199129c1afe4f4ec1906e640bec48c6 --- /dev/null +++ b/byv-command-patttern-20/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/byv-command-patttern-20/pom.xml b/byv-command-patttern-20/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..57c1ca39212d33c62fdb55302a5a2984b10ec69b --- /dev/null +++ b/byv-command-patttern-20/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + org.example + byv-command-patttern-20 + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + org.projectlombok + lombok + 1.18.28 + compile + + + org.dom4j + dom4j + 2.1.3 + + + jaxen + jaxen + 1.1.6 + + + log4j + log4j + 1.2.12 + + + org.slf4j + slf4j-api + 1.7.36 + + + com.alibaba + fastjson + 1.2.62 + + + junit + junit + 4.12 + compile + + + + + \ No newline at end of file diff --git a/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Chef.java b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Chef.java new file mode 100644 index 0000000000000000000000000000000000000000..6651857d8de6d0ccfd016212e6e187ac04608c30 --- /dev/null +++ b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Chef.java @@ -0,0 +1,17 @@ +package com.boyunv.command.example01; +/* + *@description + * 厨师类===>receiver + *@author boyunv + *@create 2023/9/7 7:29 + *@version 1.0 + */ + +public class Chef { + + public void makeFood(int num,String foodName){ + System.out.println(num+"份"+foodName); + + + } +} diff --git a/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Client.java b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Client.java new file mode 100644 index 0000000000000000000000000000000000000000..ceb538aba22c883ec07ff716fa701a7316b525fa --- /dev/null +++ b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Client.java @@ -0,0 +1,40 @@ +package com.boyunv.command.example01; +/* + *@description + * + *@author boyunv + *@create 2023/9/7 7:42 + *@version 1.0 + */ + +import java.sql.Wrapper; + +public class Client { + public static void main(String[] args) throws InterruptedException { + Order order = new Order(); + order.setDiningTable(10); + order.getFoodMenu().put("暴雨炒饭",1); + order.getFoodMenu().put("暴雨00炒面",1); + + + Order order2= new Order(); + order2.setDiningTable(10); + order2.getFoodMenu().put("黄焖鸡米饭",1); + order2.getFoodMenu().put("银耳汤",1); + + + //创建接受者 + Chef chef = new Chef(); + //将订单和接受者对象封装成命令对象 + OrderCommand cmd1 = new OrderCommand(chef, order); + OrderCommand cmd2 = new OrderCommand(chef, order2); + + //创建调用者 + Waiter waiter = new Waiter(); + waiter.setCommands(cmd1); + waiter.setCommands(cmd2); + + //将订单发送给厨师 上菜 + waiter.orderUp(); + } +} diff --git a/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Command.java b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Command.java new file mode 100644 index 0000000000000000000000000000000000000000..88c69f5ffab4fb6efd999211c936b1eb0a8a8d30 --- /dev/null +++ b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Command.java @@ -0,0 +1,14 @@ +package com.boyunv.command.example01; +/* + *@description + * 抽象命令接口 + *@author boyunv + *@create 2023/9/7 7:30 + *@version 1.0 + */ + +public interface Command { + + void execute() throws InterruptedException; //统一的执行方法 + +} diff --git a/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Order.java b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Order.java new file mode 100644 index 0000000000000000000000000000000000000000..c89c3c9b800527da4a25a04b300c2432ee40af15 --- /dev/null +++ b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Order.java @@ -0,0 +1,24 @@ +package com.boyunv.command.example01; +/* + *@description + * 订单类 + *@author boyunv + *@create 2023/9/7 7:26 + *@version 1.0 + */ + +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; +@Data +public class Order { + + private int diningTable; //餐桌号码 + + private Map foodMenu=new HashMap<>(); //存储菜名和份数 + + + + +} diff --git a/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/OrderCommand.java b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/OrderCommand.java new file mode 100644 index 0000000000000000000000000000000000000000..62f208bd9eecb93a7f6f3be2bf9045a4dd6e69ce --- /dev/null +++ b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/OrderCommand.java @@ -0,0 +1,36 @@ +package com.boyunv.command.example01; +/* + *@description + * 具体命令 + *@author boyunv + *@create 2023/9/7 7:32 + *@version 1.0 + */ + +import java.util.Set; + +public class OrderCommand implements Command{ + + //接受者对象引用 + private Chef receiver; + + private Order order; + + public OrderCommand(Chef receiver, Order order) { + this.receiver = receiver; + this.order = order; + } + + @Override + public void execute() throws InterruptedException { + System.out.println(order.getDiningTable()+"桌的订单"); + + Set menuKey = order.getFoodMenu().keySet(); + for (String menu : menuKey) { + receiver.makeFood(order.getFoodMenu().get(menu),menu); + } + + Thread.sleep(1000); + System.out.println(order.getDiningTable()+"桌的菜品已经上齐"); + } +} diff --git a/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Waiter.java b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Waiter.java new file mode 100644 index 0000000000000000000000000000000000000000..8ccec10705497c4e5eb28d7a1958b1f0ea1d52e3 --- /dev/null +++ b/byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Waiter.java @@ -0,0 +1,37 @@ +package com.boyunv.command.example01; +/* + *@description + * 服务员==>Invoker 调用者 + *@author boyunv + *@create 2023/9/7 7:38 + *@version 1.0 + */ + +import java.util.ArrayList; + +public class Waiter { + + //可以持有多个命令对象 + private ArrayList commands; + public Waiter(){ + this.commands=new ArrayList<>(); + + } + + public Waiter(ArrayList commands) { + this.commands = commands; + } + public void setCommands(Command command){ + this.commands.add(command); + } + + //发出指令 + public void orderUp() throws InterruptedException { + System.out.println("叮咚!有新的订单,厨师制作"); + for (Command command : commands) { + if(command!=null){ + command.execute(); + } + } + } +} diff --git a/byv-command-patttern-20/src/main/resources/config.xml b/byv-command-patttern-20/src/main/resources/config.xml new file mode 100644 index 0000000000000000000000000000000000000000..7cbe016568f295adf6621d9d76f83604173b20e5 --- /dev/null +++ b/byv-command-patttern-20/src/main/resources/config.xml @@ -0,0 +1,4 @@ + + + com.boyunv.strategy.example03.MT1101HandlerStrategy + \ No newline at end of file