命令模式

canca17年前 (2007-03-26)设计模式296
//命令模式
//CopyRight(C) CAnca software Office.2006
//Created by CAnca.
public class CommandMode{
 public static void main(String[] args){
  Receiver receiver = new Receiver();
  Command command = new ConcreteCommand(receiver);
  Invoker invoker = new Invoker();
  invoker.setCommand(command);
  invoker.ExecuteCommand();
 } 
}
//Command
abstract class Command{
 protected Receiver receiver;
 public Command(Receiver receiver){
  this.receiver = receiver;
 }
 abstract void Execute();
}
//ConcreteCommand
class ConcreteCommand extends Command{
 public ConcreteCommand(Receiver receiver){
  super(receiver);
 }
 
 public void Execute(){
  receiver.Action();
 }
}
//Receiver
class Receiver{
 public void Action(){
  System.out.println("Receiver.Action()");
 }
}
//Invoker
class Invoker{
 private Command command;
 public void setCommand(Command command){
  this.command = command;
 }
 
 public void ExecuteCommand(){
  command.Execute();
 }
}


相关文章

抽象工厂模式

//抽象工厂模式//Copyright CAnca Software office//Created by CAnca.2006public class AbstractFactory{ p...

工厂方法模式

//工厂方法模式:现实例子//CopyRight(C)CAnca Software Office. 2006//Created by CAnca. import java.util.*; public...

建造者模式

//建造者模式//CopyRight(C)CAnca Software Office.//Created by CAnca. import java.util.*; public class Buil...

原型模式

//原型模式//CopyRight(C) CAnca Software Office.2006//Created by CAnca. public class PrototypeMode{ ...

桥接模式

//桥接模式//CopyRight(C) CAnca Software Office 2006.//Created by CAnca. import java.util.*; public class...

适配器模式

//适配器模式//CopyRight(C)CAnca Software Office.//Created by CAnca. public class AdapterMode{ public...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。