一、什么是命令模式
Command模式也叫命令模式 ,是行为设计模 式的一种。Command模式通过被称为 Command的类封装了对目标对象的调用行为以
及调用参数。
二、命令模式的应用场景
在面向对象的程序设计中,一个对象调用另一个对象, 一般情况下的调用过程是:创建目标对象实例;设置调
用参数;调用目标对象的方法。但在有些情况下有必要使用一个专门的类对这种调用 过程加以封装,我们把这种专门的类称作command类。-
整个调用过程比较繁杂,或者存在多处这种调用。 这时,使用Command类对该调用加以封装,便于功能的 再利用。-
调用前后需要对调用参数进行某些处理。- 调用前后需要进行某些额外处理,比如日志,缓存,记录历史操作等。
三、命令模式的结构
四、命令模式的角色和职责
Command Command抽象类。 ConcreteCommand Command的具体实现类。 Receiver
需要被调用的目标对象。 Invorker 通过Invorker执行Command对象。
//示例1:
/*
* 小商贬
*/
public class Peddler {
//卖苹果
public void sellApple(){
System.out.println("卖苹果");
}
//卖香焦
public void sellBanana(){
System.out.println("卖香焦");
}
}
public class MainClass {
public static void main(String[] args) {
Peddler peddler = new Peddler();
peddler.sellApple();
peddler.sellBanana();
}
}
//卖苹果
//卖香焦
//示例2:
/*
* 小商贬
*/
public class Peddler {
//卖苹果
public void sellApple(){
System.out.println("卖苹果");
}
//卖香焦
public void sellBanana(){
System.out.println("卖香焦");
}
}
public abstract class Command {
protected Peddler peddler;
public void setPeddler(Peddler peddler) {
this.peddler = peddler;
}
public Command(Peddler peddler){
this.peddler = peddler;
}
public abstract void sell();
}
public class AppleCommand extends Command {
public AppleCommand(Peddler peddler) {
super(peddler);
}
@Override
public void sell() {
peddler.sellApple();
}
}
public class BananaCommand extends Command {
public BananaCommand(Peddler peddler) {
super(peddler);
}
@Override
public void sell() {
peddler.sellBanana();
}
}
public class MainClass {
public static void main(String[] args) {
Peddler peddler = new Peddler();
Command appleCommand = new AppleCommand(peddler);
Command bananaCommand = new BananaCommand(peddler);
appleCommand.sell();
bananaCommand.sell();
}
}
//卖苹果
//卖香焦
//示例3:
/*
* 小商贬
*/
public class Peddler {
//卖苹果
public void sellApple(){
System.out.println("卖苹果");
}
//卖香焦
public void sellBanana(){
System.out.println("卖香焦");
}
}
public abstract class Command {
protected Peddler peddler;
public void setPeddler(Peddler peddler) {
this.peddler = peddler;
}
public Command(Peddler peddler){
this.peddler = peddler;
}
public abstract void sell();
}
public class AppleCommand extends Command {
public AppleCommand(Peddler peddler) {
super(peddler);
}
@Override
public void sell() {
peddler.sellApple();
}
}
public class BananaCommand extends Command {
public BananaCommand(Peddler peddler) {
super(peddler);
}
@Override
public void sell() {
peddler.sellBanana();
}
}
public class Waiter {
private Command command;
public Command getCommand() {
return command;
}
public void setCommand(Command command) {
this.command = command;
}
public void sell(){
command.sell();
}
}
public class MainClass {
public static void main(String[] args) {
Peddler peddler = new Peddler();
Command appleCommand = new AppleCommand(peddler);
Command bananaCommand = new BananaCommand(peddler);
Waiter waiter = new Waiter();
waiter.setCommand(appleCommand);
waiter.sell();
waiter.setCommand(bananaCommand);
waiter.sell();
}
}
//卖苹果
//卖香焦