这个命令模式一写就停不下来了,想着完善功能,结果发现我能一直添加功能进去,于是了一会,发现不能再写了,再写就成为一个点餐小项目了,于是就简单写了点,融合了单例模式。
首先设计一个命令抽象类,然后两个具体命令继承该抽象类,再设计一个命令执行对象基类,两个具体执行类继承该基类,接着设计一个invoker负责连接客户类和命令类,再设计一个额外的类加入业务需要,最后是一个主类。下面是具体代码
命令抽象类:
package com.freshbin.pattern.command.myexample.command;
import com.freshbin.pattern.command.myexample.receive.ReceiverBase;
/**
* 命令抽象类
*
* @author freshbin
* @date 2019-1-5 11:24:33
*/
public abstract class Command {
protected ReceiverBase receiverBase;
public Command(ReceiverBase receiverBase) {
this.receiverBase = receiverBase;
}
public abstract void executeCommand();
public abstract Integer getFoodStock();
public abstract void setFoodStock();
public abstract String display();
}
两个命令类
package com.freshbin.pattern.command.myexample.command;
import com.freshbin.pattern.command.myexample.food.FoodManager;
import com.freshbin.pattern.command.myexample.receive.ReceiverBase;
/**
* 烤羊肉串命令类
*
* @author freshbin
* @date 2019-1-5 11:42:29
*/
public class BakeMuttonCommand extends Command {
public BakeMuttonCommand(ReceiverBase receiverBase) {
super(receiverBase);
}
@Override
public void executeCommand() {
receiverBase.bakeMutton();
}
@Override
public Integer getFoodStock() {
FoodManager foodManager = FoodManager.getFoodManager();
return foodManager.getMutton();
}
@Override
public String display() {
return "烤羊肉串";
}
@Override
public void setFoodStock() {
FoodManager foodManager = FoodManager.getFoodManager();
Integer foodStock = getFoodStock();
foodManager.setMutton(--foodStock);
}
}
package com.freshbin.pattern.command.myexample.command;
import com.freshbin.pattern.command.myexample.food.FoodManager;
import com.freshbin.pattern.command.myexample.receive.ReceiverBase;
/**
* 烤鸡翅命令类
*
* @author freshbin
* @date 2019-1-5 11:42:29
*/
public class BakeChickenWingCommand extends Command {
public BakeChickenWingCommand(ReceiverBase receiverBase) {
super(receiverBase);
}
@Override
public void executeCommand() {
receiverBase.bakeChickenWing();
}
@Override
public Integer getFoodStock() {
FoodManager foodManager = FoodManager.getFoodManager();
return foodManager.getChickenWing();
}
@Override
public void setFoodStock() {
FoodManager foodManager = FoodManager.getFoodManager();
Integer foodStock = getFoodStock();
foodManager.setChickenWing(--foodStock);
}
@Override
public String display() {
return "烤鸡翅";
}
}
命令执行基类和子类
package com.freshbin.pattern.command.myexample.receive;
/**
* 命令执行对象基类
*
* @author freshbin
* @date 2019-1-5 11:37:59
*/
public abstract class ReceiverBase {
public abstract void bakeMutton();
public abstract void bakeChickenWing();
}
package com.freshbin.pattern.command.myexample.receive;
import com.freshbin.pattern.command.myexample.food.FoodManager;
/**
* 命令执行对象
*
* @author freshbin
* @date 2019-1-5 11:29:17
*/
public class BarbecuerOne extends ReceiverBase {
public void bakeMutton() {
FoodManager foodManager = FoodManager.getFoodManager();
Integer muttonOld = foodManager.getMutton();
System.out.println("库存羊肉串数量:" + muttonOld);
System.out.println("师傅甲开始烤羊肉串!");
Integer muttonNew = --muttonOld;
foodManager.setMutton(muttonNew);
System.out.println("剩下的羊肉串数量:" + muttonNew);
}
public void bakeChickenWing() {
FoodManager foodManager = FoodManager.getFoodManager();
Integer chickenWingOld = foodManager.getChickenWing();
System.out.println("库存鸡翅数量:" + chickenWingOld);
System.out.println("师傅甲开始烤鸡翅!");
Integer chickenWingNew = --chickenWingOld;
foodManager.setChickenWing(chickenWingNew);
System.out.println("剩下的鸡翅数量:" + chickenWingNew);
}
}
package com.freshbin.pattern.command.myexample.receive;
import com.freshbin.pattern.command.myexample.food.FoodManager;
/**
* 命令执行对象
*
* @author freshbin
* @date 2019-1-5 11:29:17
*/
public class BarbecuerTwo extends ReceiverBase {
public void bakeMutton() {
FoodManager foodManager = FoodManager.getFoodManager();
Integer muttonOld = foodManager.getMutton();
System.out.println("库存羊肉串数量:" + muttonOld);
System.out.println("师傅乙开始烤羊肉串!");
Integer muttonNew = --muttonOld;
foodManager.setMutton(muttonNew);
System.out.println("剩下的羊肉串数量:" + muttonNew);
}
public void bakeChickenWing() {
FoodManager foodManager = FoodManager.getFoodManager();
Integer chickenWingOld = foodManager.getChickenWing();
System.out.println("库存鸡翅数量:" + chickenWingOld);
System.out.println("师傅乙开始烤鸡翅!");
Integer chickenWingNew = --chickenWingOld;
foodManager.setChickenWing(chickenWingNew);
System.out.println("剩下的鸡翅数量:" + chickenWingNew);
}
}
再来一个invoker,服务员类
package com.freshbin.pattern.command.myexample.invoker;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.freshbin.pattern.command.myexample.command.Command;
/**
* 服务员类
*
* @author freshbin
* @date 2019-1-5 11:53:01
*/
public class Waiter {
private List<Command> commands = new ArrayList<>();
private Map<Command, Integer> foodStockMap = new HashMap<>();
public void setCommand(Command command) {
if (!validFoodStock(command)) {
return;
}
commands.add(command);
}
private Boolean validFoodStock(Command command) {
Integer foodStock = 0;
if (foodStockMap.containsKey(command)) {
foodStock = foodStockMap.get(command);
} else {
foodStock = command.getFoodStock();
}
if (foodStock <= 0) {
System.out.println("服务员反馈:" + command.display() + "没有了!");
return false;
}
foodStockMap.put(command, --foodStock);
return true;
}
public void cancleCommand(Command command) {
commands.remove(command);
}
public void notifyCommand() {
for (Command command : commands) {
command.executeCommand();
}
}
}
业务需要的食物管理类
package com.freshbin.pattern.command.myexample.food;
import com.freshbin.pattern.command.myexample.receive.ReceiverBase;
/**
* 食物管理类
*
* @author freshbin
* @date 2019-1-5 12:06:48
*/
public class FoodManager {
private Integer mutton = 10;
private Integer chickenWing = 10;
private volatile static FoodManager foodManager = null;
private FoodManager() {
}
public static FoodManager getFoodManager() {
if (foodManager == null) {
synchronized (ReceiverBase.class) {
if (foodManager == null) {
foodManager = new FoodManager();
}
}
}
return foodManager;
}
public Integer getMutton() {
return mutton;
}
public void setMutton(Integer mutton) {
this.mutton = mutton;
}
public Integer getChickenWing() {
return chickenWing;
}
public void setChickenWing(Integer chickenWing) {
this.chickenWing = chickenWing;
}
}
客户端主类
package com.freshbin.pattern.command.myexample;
import com.freshbin.pattern.command.myexample.command.BakeChickenWingCommand;
import com.freshbin.pattern.command.myexample.command.BakeMuttonCommand;
import com.freshbin.pattern.command.myexample.command.Command;
import com.freshbin.pattern.command.myexample.invoker.Waiter;
import com.freshbin.pattern.command.myexample.receive.BarbecuerOne;
import com.freshbin.pattern.command.myexample.receive.BarbecuerTwo;
import com.freshbin.pattern.command.myexample.receive.ReceiverBase;
/**
* 命令模式
*
* @author freshbin
* @date 2019-1-5 11:23:27
*/
public class CommandMain {
public static void main(String[] args) {
System.out.println("===========点餐================");
ReceiverBase receiverBaseOne = new BarbecuerOne();
ReceiverBase receiverBaseTwo = new BarbecuerTwo();
Command commandOne = new BakeChickenWingCommand(receiverBaseOne);
Command commandTwo = new BakeMuttonCommand(receiverBaseTwo);
Waiter waiterOne = new Waiter();
for (int i = 0; i < 12; i++) {
waiterOne.setCommand(commandOne);
}
waiterOne.setCommand(commandTwo);
waiterOne.cancleCommand(commandOne);
waiterOne.notifyCommand();
System.out.println("================");
System.out.println("===========点餐================");
Waiter waiterTwo = new Waiter();
for (int j = 0; j < 10; j++) {
waiterTwo.setCommand(commandOne);
waiterTwo.setCommand(commandTwo);
}
waiterTwo.notifyCommand();
System.out.println("================");
}
}
然后是演示效果图: