当前位置:首页 > 设计模式 > 正文内容

命令模式

canca19年前 (2007-03-26)设计模式471
//命令模式
//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();
 }
}


扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/651.html

标签: 设计模式
分享给朋友:

“命令模式” 的相关文章

抽象工厂模式

//抽象工厂模式//Copyright CAnca Software office//Created by CAnca.2006public class AbstractFactory{ public static void main(String[] args){  ...

工厂方法模式

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

建造者模式

//建造者模式//CopyRight(C)CAnca Software Office.//Created by CAnca. import java.util.*; public class BuilderMode{ public static void main(String[] arg...

原型模式

//原型模式//CopyRight(C) CAnca Software Office.2006//Created by CAnca. public class PrototypeMode{ public static void main(String[] args){  ...

代理模式

//代理模式--真实例子//CopyRight(C)CAnca Software Office.2006//Created by CAnca.   public class ProxyMode_RealWorld{     public static void...

桥接模式

//桥接模式//CopyRight(C) CAnca Software Office 2006.//Created by CAnca. import java.util.*; public class BridgeMode{ public static void main(String[]...

发表评论

访客

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