package com.eyugame.modle; /** * 命令模式 * @author JYC506 * */ public class MyCommand implements ICommand{ private Receiver receiver; public MyCommand(Receiver receiver) { super(); this.receiver = receiver; } @Override public void execute() { this.receiver.doSomeThing(); } public static void main(String[] args) { /*创建命令接收执行者*/ Receiver receiver=new Receiver(); /*创建命令并指定接收者*/ ICommand command=new MyCommand(receiver); /*创建命令发布者*/ Invoker invoker=new Invoker(command); /*发布命令*/ invoker.releaseCommand(); } } /*命令接口*/ interface ICommand{ void execute(); } /*发布名字者*/ class Invoker{ private ICommand command; public Invoker(ICommand command) { super(); this.command = command; } public void releaseCommand(){ this.command.execute(); } } /*命令接收者*/ class Receiver{ public void doSomeThing(){ System.out.println("接受者执行命令"); } }
原文地址:http://blog.csdn.net/h348592532/article/details/45307225