将一个请求封装为一个对象,使发出请求的责任和执行请求的责任分割开。这样两者之间通过命令对象进行沟通,这样方便将命令对象进行存储、传递、调用、增加与管理。
命令模式包含以下主要角色
服务员: 就是调用者角色,由她来发起命令。
资深大厨: 就是接收者角色,真正命令执行的对象。
订单: 命令中包含订单。
/**
* @author 晓风残月Lx
* @date 2023/7/27 0:58
* 抽象命令类
*/
public interface Command {
void execute(); // 只需要定义一个统一的执行方法
}
import java.util.Map;
import java.util.Set;
/**
* @author 晓风残月Lx
* @date 2023/7/27 1:04
* 具体的命令类
*/
public class OrderCommand implements Command {
// 持有接收者对象
private SeniorChef receiver;
private Order order;
public OrderCommand(SeniorChef receiver, Order order) {
this.receiver = receiver;
this.order = order;
}
@Override
public void execute() {
System.out.println(order.getDiningTables() + "桌的订单:");
Map<String, Integer> foodDir = order.getFoodDir();
// 遍历集合
Set<String> keys = foodDir.keySet();
for (String foodName : keys) {
receiver.makeFood(foodName, foodDir.get(foodName));
}
System.out.println(order.getDiningTables() + "桌的饭ok了");
}
}
import java.util.HashMap;
import java.util.Map;
/**
* @author 晓风残月Lx
* @date 2023/7/27 1:00
* 订单类
*/
public class Order {
// 餐桌号码
private int diningTables;
// 所下的餐品及份数
private Map<String, Integer> foodDir = new HashMap<String, Integer>();
public int getDiningTables() {
return diningTables;
}
public void setDiningTables(int diningTables) {
this.diningTables = diningTables;
}
public Map<String, Integer> getFoodDir() {
return foodDir;
}
public void setFood(String name, int num) {
foodDir.put(name, num);
}
}
/**
* @author 晓风残月Lx
* @date 2023/7/27 1:03
* 厨子类
*/
public class SeniorChef {
public void makeFood(String name, int num) {
System.out.println(num + "份" + name);
}
}
import java.util.ArrayList;
import java.util.List;
/**
* @author 晓风残月Lx
* @date 2023/7/27 1:08
* 服务员类 请求者角色
*/
public class Waitor {
// 持有多个命令对象
private List<Command> commands = new ArrayList<>();
public void setCommands(Command command) {
// 将对象存储到list集合中
commands.add(command);
}
// 发起命令功能 喊 订单来了
public void orderUp() {
System.out.println("后厨,订单来了");
// 遍历list集合
for (Command command : commands) {
if (command != null) {
command.execute();
}
}
}
}
/**
* @author 晓风残月Lx
* @date 2023/7/27 1:11
*/
public class Client {
public static void main(String[] args) {
// 创建一个订单对象
Order order = new Order();
order.setDiningTables(1);
order.setFood("米饭", 1);
order.setFood("可乐", 2);
Order order2 = new Order();
order2.setDiningTables(2);
order2.setFood("面条", 2);
order2.setFood("芬达", 1);
// 创建厨师对象
SeniorChef receiver = new SeniorChef();
// 创建命令对象
OrderCommand orderCommand = new OrderCommand(receiver, order);
OrderCommand orderCommand2 = new OrderCommand(receiver, order2);
// 创建调用者
Waitor waitor = new Waitor();
waitor.setCommands(orderCommand);
waitor.setCommands(orderCommand2);
// 服务员发起命令
waitor.orderUp();
}
}
优点:
缺点:
Runable是一个典型命令模式,Runnable担当命令的角色,Thread充当的是调用者,start方法就是其执行方法
//命令接口(抽象命令角色)
public interface Runnable {
public abstract void run();
}
//调用者
public class Thread implements Runnable {
private Runnable target;
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
}
}
}
private native void start0();
}
会调用一个native方法start0(),调用系统方法,开启一个线程。而接收者是对程序员开放的,可以自己定义接收者。
/**
* jdk Runnable 命令模式
* TurnOffThread : 属于具体
*/
public class TurnOffThread implements Runnable{
private Receiver receiver;
public TurnOffThread(Receiver receiver) {
this.receiver = receiver;
}
public void run() {
receiver.turnOFF();
}
}
/**
* 测试类
*/
public class Demo {
public static void main(String[] args) {
Receiver receiver = new Receiver();
TurnOffThread turnOffThread = new TurnOffThread(receiver);
Thread thread = new Thread(turnOffThread);
thread.start();
}
}