本文继续介绍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.
原文地址:http://blog.csdn.net/jason0539/article/details/45110355