From a961b8d5bed9ff3fdd326244a9a2d1272d4f825f Mon Sep 17 00:00:00 2001 From: "1923365156@qq.com" <$$@@zhaofan666> Date: Thu, 7 Sep 2023 07:51:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=BD=E4=BB=A4=E6=A8=A1=E5=BC=8F=E7=9A=84?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=AE=9E=E4=BE=8B=E4=BB=A5=E5=8F=8A=E6=80=BB?= =?UTF-8?q?=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 2 + .idea/encodings.xml | 2 + .idea/misc.xml | 1 + README.md | 17 ++++++ byv-command-patttern-20/.gitignore | 38 ++++++++++++ byv-command-patttern-20/pom.xml | 58 +++++++++++++++++++ .../com/boyunv/command/example01/Chef.java | 17 ++++++ .../com/boyunv/command/example01/Client.java | 40 +++++++++++++ .../com/boyunv/command/example01/Command.java | 14 +++++ .../com/boyunv/command/example01/Order.java | 24 ++++++++ .../command/example01/OrderCommand.java | 36 ++++++++++++ .../com/boyunv/command/example01/Waiter.java | 37 ++++++++++++ .../src/main/resources/config.xml | 4 ++ 13 files changed, 290 insertions(+) create mode 100644 byv-command-patttern-20/.gitignore create mode 100644 byv-command-patttern-20/pom.xml create mode 100644 byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Chef.java create mode 100644 byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Client.java create mode 100644 byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Command.java create mode 100644 byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Order.java create mode 100644 byv-command-patttern-20/src/main/java/com/boyunv/command/example01/OrderCommand.java create mode 100644 byv-command-patttern-20/src/main/java/com/boyunv/command/example01/Waiter.java create mode 100644 byv-command-patttern-20/src/main/resources/config.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 64112cd..f28b7ce 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 c156c3c..b54dff9 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -5,6 +5,8 @@ + + diff --git a/.idea/misc.xml b/.idea/misc.xml index 513980f..d093d14 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -20,6 +20,7 @@ diff --git a/README.md b/README.md index bc1a05f..233e91b 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 0000000..5ff6309 --- /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 0000000..57c1ca3 --- /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 0000000..6651857 --- /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 0000000..ceb538a --- /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 0000000..88c69f5 --- /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 0000000..c89c3c9 --- /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 0000000..62f208b --- /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 0000000..8ccec10 --- /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 0000000..7cbe016 --- /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 -- Gitee