- 将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。
package design.command;
/**
* @Author: hyh
* @Date: 2021/8/17 20:19
* 抽象一个机器人
**/
public abstract class Robot {
public abstract void doThing();
}
/**
* 跳舞接收器接受命令执行
**/
class Dance extends Robot {
@Override
public void doThing() {
System.out.println("机器人跳舞");
}
}
/**
* 唱歌接收器接受命令执行
**/
class Sing extends Robot {
@Override
public void doThing() {
System.out.println("机器人唱歌");
}
}
package design.command;
/**
* 抽象命令接口
**/
abstract class Command {
// 执行命令
public abstract void execute();
}
/**
* 跳舞命令
**/
class DanceCommand extends Command {
private Robot r;
public DanceCommand(Robot r) {
this.r = r;
}
@Override
public void execute() {
r.doThing();
}
}
// 唱歌命令
class SingCommand extends Command {
private Robot r;
public SingCommand(Robot r) {
this.r = r;
}
@Override
public void execute() {
r.doThing();
}
}
// 我
class Me {
private Command c;
public void setCommand(Command c) {
this.c = c;
}
public void action() {
this.c.execute();
}
public static void main(String[] args) {
// new 一个自己
Me me = new Me();
// new 一个接收器
Robot sing = new Sing();
// 发送唱歌命令
Command singCommand = new SingCommand(sing);
// 自己发命令
me.setCommand(singCommand);
// 执行
me.action();
}
}
输出:
机器人唱歌
修改后:
package command;
/**
* @Author: hyh
* @Date: 2021/8/17 20:19
* 抽象一个机器人
**/
public abstract class Robot {
public abstract void doThing();
}
/**
* 接收跳舞命令执行
**/
class Dance extends Robot {
@Override
public void doThing() {
System.out.println("机器人跳舞");
}
}
/**
* 接收唱歌命令执行
**/
class Sing extends Robot {
@Override
public void doThing() {
System.out.println("机器人唱歌");
}
}
/**
* 抽象命令接口
**/
abstract class Command {
// 唱歌功能
protected Sing s = new Sing();
// 跳舞功能
protected Dance d = new Dance();
// 执行命令
public abstract void execute();
}
/**
* 跳舞命令
**/
class DanceCommand extends Command {
@Override
public void execute() {
super.d.doThing();
}
}
// 唱歌命令
class SingCommand extends Command {
@Override
public void execute() {
super.s.doThing();
//当然也可以既场各又跳舞
//super.d.doThing();
}
}
// 我
class Me {
private Command c;
public void setCommand(Command c) {
this.c = c;
}
public void action() {
this.c.execute();
}
public static void main(String[] args) {
// new 一个自己
Me me = new Me();
// 发送唱歌命令
Command singCommand = new SingCommand();
// 自己发命令
me.setCommand(singCommand);
// 执行
me.action();
}
}
输出:
机器人唱歌
class Me {
private Command c;
public void setCommand(Command c) {
this.c = c;
}
public void action() {
this.c.execute();
}
public static void main(String[] args) {
// new 一个自己
Me me = new Me();
// 发送唱歌命令
Command danceCommand = new DanceCommand();
// 自己发命令
me.setCommand(danceCommand);
// 执行
me.action();
}
}
输出:
机器人跳舞
再次修改:
package command;
/**
* @Author: hyh
* @Date: 2021/8/17 20:19
* 抽象一个机器人
**/
public abstract class Robot {
public abstract void doThing();
}
/**
* 接收跳舞命令执行
**/
class Dance extends Robot {
@Override
public void doThing() {
System.out.println("机器人跳舞");
}
}
/**
* 接收唱歌命令执行
**/
class Sing extends Robot {
@Override
public void doThing() {
System.out.println("机器人唱歌");
}
}
/**
* 抽象命令接口
**/
abstract class Command {
//
protected Robot r;
public Command(Robot r) {
this.r = r;
}
// 执行命令
public abstract void execute();
}
/**
* 跳舞命令
**/
class DanceCommand extends Command {
public DanceCommand(){
super(new Dance());
}
@Override
public void execute() {
super.r.doThing();
}
}
// 唱歌命令
class SingCommand extends Command {
public SingCommand(){
super(new Sing());
}
@Override
public void execute() {
super.r.doThing();
}
}
// 我
class Me {
private Command c;
public void setCommand(Command c) {
this.c = c;
}
public void action() {
this.c.execute();
}
public static void main(String[] args) {
// new 一个自己
Me me = new Me();
// 发送唱歌命令
Command danceCommand = new DanceCommand();
// 自己发命令
me.setCommand(danceCommand);
// 执行
me.action();
}
}
输出:
机器人跳舞