概念
命令模式(Command Pattern)是一种数据驱动的设计模式,它属于行为型模式。请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。
参考链接: 命令模式 | 菜鸟教程
代码案例
创建总公司发布命令的接口
/**
* 总公司
*/
public interface CentralOffice {
/**
* 发布命令
* @return
* @param name
* @param count
* @param colour
*/
String execute(String name,int count,String colour);
}
创建自行车命令执行类
/**
* 自行车请求类
*/
@Slf4j
public class Bicycle {
/**
* 生产自行车
* @param name
* @param count
* @param colour
* @return
*/
public String produce(String name,int count,String colour){
log.info("生产{}辆{}色{}自行车",count,colour,name);
return "生产"+count+"辆"+colour+"色"+name+"自行车";
}
/**
* 卖出自行车
* @param name
* @param count
* @param colour
* @return
*/
public String sell(String name,int count,String colour){
log.info("卖出{}辆{}色{}自行车",count,colour,name);
return "卖出"+count+"辆"+colour+"色"+name+"自行车";
}
}
两个命令执行实现类
生产自行车
/**
* 生产自行车实现类
*/
public class ProduceBicycle implements CentralOffice {
private Bicycle bicycle;
public ProduceBicycle(Bicycle bicycle) {
this.bicycle = bicycle;
}
@Override
public String execute(String name, int count, String colour) {
return bicycle.produce(name,count,colour);
}
}
销售自行车
/**
* 销售自行车实现类
*/
public class SellBicycle implements CentralOffice {
private Bicycle bicycle;
public SellBicycle(Bicycle bicycle) {
this.bicycle = bicycle;
}
@Override
public String execute(String name, int count, String colour) {
return bicycle.sell(name,count,colour);
}
}
创建容器类,有添加命令,执行所有命令方法
/**
* 创建命令调用类
*/
public class Queues {
List<CentralOffice> centralOffices = new ArrayList<>();
/**
* 往命令集合新增
*
* @param centralOffice
*/
public void add(CentralOffice centralOffice) {
centralOffices.add(centralOffice);
}
/**
* 获取集合元素并执行每个里面的指令
* @param name
* @param count
* @param colour
* @return
*/
public List<String> getAndExecute(String name, int count, String colour) {
List<String> resultList = new ArrayList<>();
for (CentralOffice centralOffice : centralOffices) {
String execute = centralOffice.execute(name, count, colour);
resultList.add(execute);
}
centralOffices.clear();
return resultList;
}
}
实体类
/**
* 自行车实体类
*/
@Setter
@Getter
public class BicycleEntity {
/**
* 品牌
*/
private String name;
/**
* 数量
*/
private int count;
/**
* 颜色
*/
private String colour;
}
测试主类
/**
* 设计模式控制器
*/
@RestController
@RequestMapping("/designPattern")
@Slf4j
public class DesignController {
@PostMapping("/command")
public ResponseModel command(@RequestBody BicycleEntity bicycleEntities) {
log.info("command ---- start ");
com.koukay.student.design.command.Bicycle bicycle = new com.koukay.student.design.command.Bicycle();
ProduceBicycle produceBicycle= new ProduceBicycle(bicycle);
SellBicycle sellBicycle=new SellBicycle(bicycle);
Queues queues= new Queues();
queues.add(produceBicycle);
queues.add(sellBicycle);
List<String> re = queues.getAndExecute(bicycleEntities.getName(),
bicycleEntities.getCount(), bicycleEntities.getColour());
log.info(JSON.toJSONString(re,true));
log.info("command ---- end ");
return new ResponseModel("命令模式完成", 200, re);
}
}
测试案例
2022-06-28 01:05:10.677 INFO command ---- start 【http-nio-8081-exec-2】【DesignController:72】
2022-06-28 01:05:10.681 INFO 生产10辆白色Giant自行车 【http-nio-8081-exec-2】【Bicycle:18】
2022-06-28 01:05:10.681 INFO 卖出10辆白色Giant自行车 【http-nio-8081-exec-2】【Bicycle:30】
2022-06-28 01:05:10.681 INFO [
"生产10辆白色Giant自行车",
"卖出10辆白色Giant自行车"
] 【http-nio-8081-exec-2】【DesignController:83】
2022-06-28 01:05:10.682 INFO 生产20辆蓝色Merida自行车 【http-nio-8081-exec-2】【Bicycle:18】
2022-06-28 01:05:10.682 INFO 卖出20辆蓝色Merida自行车 【http-nio-8081-exec-2】【Bicycle:30】
2022-06-28 01:05:10.682 INFO [
"生产20辆蓝色Merida自行车",
"卖出20辆蓝色Merida自行车"
] 【http-nio-8081-exec-2】【DesignController:83】
2022-06-28 01:05:10.683 INFO 生产40辆红色Fenghuang自行车 【http-nio-8081-exec-2】【Bicycle:18】
2022-06-28 01:05:10.683 INFO 卖出40辆红色Fenghuang自行车 【http-nio-8081-exec-2】【Bicycle:30】
2022-06-28 01:05:10.683 INFO [
"生产40辆红色Fenghuang自行车",
"卖出40辆红色Fenghuang自行车"
] 【http-nio-8081-exec-2】【DesignController:83】
2022-06-28 01:05:10.683 INFO command ---- end 【http-nio-8081-exec-2】【DesignController:87】