一.模式定义
命令模式(Command Pattern):将一个请求封装成一个对象,从而让用户使用不同的请求把客户端参数化;对请求排队或者记录请求日志,以及支持可撤销的操作。
Command Pattern:Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
二.模式要素
- Command类:是一个抽象类,类中对需要执行的命令进行声明,一般来说要对外公布一个execute方法用来执行命令。
- ConcreteCommand类:Command类的实现类,对抽象类中声明的方法进行实现。
- Invoker类:调用者,负责调用命令。
- Receiver类:接收者,负责接收命令并且执行命令。
- Client类:最终的客户端调用类。
三.举例说明
我们以老师管理学生来举例吧。
目前社会上没有家长自己直接教育孩子文化课的,肯定都是将孩子交到老师手中来进行学校教育。那么此时家长就是Client角色,老师就是Invoker角色,孩子肯定就是Receiver角色了。老师在教育孩子时肯定是要管理和发号施令的,孩子也要听老师的话来认真学习。那么老师肯定会对孩子发出一些列的教育指令,比如按时上课,完成作业、参加考试等,这些都可以是ConcreteCommand。那么这样一套系统是怎么使用命令模式来组织呢?马上就写代码安排。
四.具体代码
老师 Teacher.java
package commandpattern;
/**
* author: lllddd
* created on: 2021/2/21 21:29
* description:老师
*/
public class Teacher {
private Command command;
/**
* 设置命令
*
* @param command 命令
*/
public void setCommand(Command command) {
this.command = command;
}
/**
* 发布命令
*/
public void issueCommand() {
if (command != null) {
command.execute();
} else {
System.out.println("老师还没有准备好命令哦!");
}
}
}
学生(孩子) Student.java
package commandpattern;
/**
* author: lllddd
* created on: 2021/2/21 21:29
* description:学生(孩子)
*/
public class Student {
/**
* 上课
*/
public void attencClass() {
System.out.println("乖学生上课......");
}
/**
* 做作业
*/
public void doHomework() {
System.out.println("乖学生做作业......");
}
/**
* 参加考试
*/
public void takeTest() {
System.out.println("乖学生参加考试......");
}
}
抽象教育指令 ICommand.java
package commandpattern;
/**
* author: lllddd
* created on: 2021/2/21 21:29
* description:抽象命令
*/
public abstract class Command {
// 持有学生
protected Student student;
public Command(Student student) {
// 关联学生
this.student = student;
}
/**
* 执行命令
*/
protected abstract void execute();
}
上课命令 AttendClassCommand.java
package commandpattern;
/**
* author: lllddd
* created on: 2021/2/21 21:30
* description:上课命令
*/
public class AttendClassCommand extends Command {
public AttendClassCommand(Student student) {
super(student);
}
@Override
public void execute() {
System.out.println("上课命令被发出,要求学生执行");
student.attencClass();
}
}
完成作业命令 DoHomeworkCommand.java
package commandpattern;
/**
* author: lllddd
* created on: 2021/2/21 21:30
* description:做作业命令
*/
public class DoHomeworkCommand extends Command {
public DoHomeworkCommand(Student student) {
super(student);
}
@Override
protected void execute() {
System.out.println("做作业命令被发出,要求学生执行");
student.doHomework();
}
}
参加考试命令 TakeTestCommand.java
package commandpattern;
/**
* author: lllddd
* created on: 2021/2/21 21:30
* description:参加考试命令
*/
public class TakeTestCommand extends Command {
public TakeTestCommand(Student student) {
super(student);
}
@Override
protected void execute() {
System.out.println("参加考试命令被发出,要求学生执行");
student.takeTest();
}
}
家长 Parent.java
package commandpattern;
/**
* author: lllddd
* created on: 2021/2/21 21:29
* description:家长
*/
public class Parent {
public static void main(String[] args) {
// 熊孩子闪亮登场
Student student = new Student();
// 雇一个老师负责教育孩子
Teacher teacher = new Teacher();
// 老师开始工作,发指令要求学生上课
AttendClassCommand attendClassCommand = new AttendClassCommand(student);
teacher.setCommand(attendClassCommand);
teacher.issueCommand();
// 老师发出第二个指令,老师发指令要求学生做作业
DoHomeworkCommand doHomeworkCommand = new DoHomeworkCommand(student);
teacher.setCommand(doHomeworkCommand);
teacher.issueCommand();
// 快要期末考试了,老师发指令要求学生参加考试
TakeTestCommand takeTestCommand = new TakeTestCommand(student);
teacher.setCommand(takeTestCommand);
teacher.issueCommand();
}
}
运行结果:
五.总结
1.命令模式的优点
(1)实现类间的解耦。调用者角色与接受者角色之间没有任何依赖关系,调用者实现功能时只需要调用Command抽象类的execute方法即可,不需要了解到底是哪个接收者执行。
(2)可扩展性好。Command子类可以非常方便地扩展,而调用者Invoker和高层次的模块Client不产生严重的代码耦合。
(3)命令模式结合其他模式会更优秀。命令模式可以结合责任链模式,实现命令族解析任务;结合末班模式,则可以减少Command子类的膨胀问题。
2.命令模式的缺点
Command子类的膨胀可能非常快,要在实际使用该模式时慎重评估。
3.命令模式的适用场景
只要你认为是命令的地方就可以使用命令模式。