大家好,又见面了,我是全栈君。
本文继续介绍23种设计模式系列之命令模式。
//执行命令的接口
public interface Command {
void execute();
}
//命令接收者Receiver
public class Tv {
public int currentChannel = 0;
public void turnOn() {
System.out.println("The televisino is on.");
}
public void turnOff() {
System.out.println("The television is off.");
}
public void changeChannel(int channel) {
this.currentChannel = channel;
System.out.println("Now TV channel is " + channel);
}
}
//开机命令ConcreteCommand
public class CommandOn implements Command {
private Tv myTv;
public CommandOn(Tv tv) {
myTv = tv;
}
public void execute() {
myTv.turnOn();
}
}
//关机命令ConcreteCommand
public class CommandOff implements Command {
private Tv myTv;
public CommandOff(Tv tv) {
myTv = tv;
}
public void execute() {
myTv.turnOff();
}
}
//频道切换命令ConcreteCommand
public class CommandChange implements Command {
private Tv myTv;
private int channel;
public CommandChange(Tv tv, int channel) {
myTv = tv;
this.channel = channel;
}
public void execute() {
myTv.changeChannel(channel);
}
}
//可以看作是遥控器Invoker
public class Control {
private Command onCommand, offCommand, changeChannel;
public Control(Command on, Command off, Command channel) {
onCommand = on;
offCommand = off;
changeChannel = channel;
}
public void turnOn() {
onCommand.execute();
}
public void turnOff() {
offCommand.execute();
}
public void changeChannel() {
changeChannel.execute();
}
}
//测试类Client
public class Client {
public static void main(String[] args) {
// 命令接收者Receiver
Tv myTv = new Tv();
// 开机命令ConcreteCommond
CommandOn on = new CommandOn(myTv);
// 关机命令ConcreteCommond
CommandOff off = new CommandOff(myTv);
// 频道切换命令ConcreteCommond
CommandChange channel = new CommandChange(myTv, 2);
// 命令控制对象Invoker
Control control = new Control(on, off, channel);
// 开机
control.turnOn();
// 切换频道
control.changeChannel();
// 关机
control.turnOff();
}
}
执行结果
The televisino is on.
Now TV channel is 2
The television is off.
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/121319.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...