`
hanlingbo2008
  • 浏览: 79991 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

命令(Command)模式例子2

阅读更多

命令(Command)模式例子2 -------- AudioPlayer系统

 

系统描述: 小女孩julia有一个盒式录音机,有播音play,倒带rewind,停止stop功能。julia不需要知道命令的执行细节,只需要知道在键盘上按下什么键就可以了。 用命令模式来模拟。

 

代码:

/**
 *  This class plays the role of Abstract Command
 */

public interface Command
{
	public abstract void execute ( );
}

 

/**
 *  This class plays the role of Concrete Command
 */

public class PlayCommand implements Command
{
	private AudioPlayer myAudio;

	public PlayCommand ( AudioPlayer a)
    {
		myAudio = a;
	}

	public void execute( )
    {
		myAudio.play();
	}
}

 

/**
 *  This class plays the role of Concrete Command
 */

public class RewindCommand implements Command
{
	private AudioPlayer myAudio;

	public RewindCommand ( AudioPlayer a)
    {
	    myAudio = a;
	}
	public void execute()
    {
	    myAudio.rewind();
	}
}

 

/**
 *  This class plays the role of Concrete Command
 */

public class StopCommand implements Command
{
    /**
     * @directed 
     */
    private AudioPlayer myAudio;
	
	public StopCommand ( AudioPlayer a)
    {
		myAudio = a;
	}
	public void execute( )
    {
		myAudio.stop();
	}
}

 

/**
 * 	This is the Invoker role
 */
public class Keypad
{
    /**
     * @link aggregation 
     */
	private Command playCmd;

    /**
     * @link aggregation 
     */
    private Command rewindCmd;

    /**
     * @link aggregation 
     */
    private Command stopCmd;
	
	public Keypad(Command play, Command stop, Command rewind)
	{
        // concrete Command registers itself with the invoker
		playCmd = play;
		stopCmd = stop;
        rewindCmd = rewind;
	}
	public void play()
	{
		playCmd.execute();
	}
	public void stop()
	{
		stopCmd.execute();
	}
    public void rewind()
    {
        rewindCmd.execute();
    }
}

 

/**
 *  This class plays the role of Receiver
 */

public class AudioPlayer
{
	public void play( )
	{
		System.out.println("Playing...");
	}

	public void rewind( )
	{
		System.out.println("Rewinding...");
	}
	
	public void stop()
	{
		System.out.println("Stopped.");
	}                                                                    
}

 

/**
 * 	This is the Client role
 */
public class Julia
{
    /**
     * @link aggregation 
     */
    private static Keypad keypad ;

    /**
     * @link aggregation 
     */
    private static AudioPlayer myAudio = new AudioPlayer();

	public static void main(String[] args)
	{
    	test1();
	}

    private static void test1()
    {
    	Command play = new PlayCommand(myAudio);
        Command stop = new StopCommand(myAudio);
        Command rewind = new RewindCommand(myAudio);

        keypad = new Keypad(play, stop, rewind);

        keypad.play();
        keypad.stop();
        keypad.rewind();

        keypad.stop();
        keypad.play();
        keypad.stop();
    }
}               

 

分享到:
评论

相关推荐

    命令模式(Command Pattern).rar

    用最简单的例子理解命令模式(Command Pattern) 命令模式的需求源自想通过一个指令(比如这里IControl的Execute方法)来控制多个类的多个方法,包含了几个要素: 1、命令:让类的各种方法抽象成类实现一个接口 2、...

    C#命令模式(Command Pattern)实例教程

    主要介绍了C#命令模式(Command Pattern),以实例的形式讲述了命令模式通过一个指令来控制多个类的多个方法,需要的朋友可以参考下

    C#设计模式.PDF

    一、 命令(Command)模式 168 二、 命令模式的结构 168 三、 命令模式的示意性源代码 169 四、 玉帝传美猴王上天 172 五、 命令模式的实现 172 六、 命令模式的实际应用案例 173 七、 在什么情况下应当使用命令模式...

    C#设计模式大全

    一、 命令(Command)模式 二、 命令模式的结构 三、 命令模式的示意性源代码 四、 玉帝传美猴王上天 五、 命令模式的实现 六、 命令模式的实际应用案例 七、 在什么情况下应当使用命令模式 八、 使用命令...

    研磨设计模式(完整带书签).part2.pdf

    第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式...

    ng-command:AngularJS的命令模式

    ng-command可让您编写仅在执行期间执行一次以及是否满足canExecute命令。 例子 var app = angular . module ( 'commandLab' , [ 'ng-command' ] ) . controller ( 'CommandExampleCtrl' , [ '$command' , '$scope...

    Hpsyche#note#6.命令模式1

    例子父实体类音响public class Stereo {命令接口public interface Command {具体命令空命令public class N

    Command-Pattern:这个项目是命令模式的一个例子。 该项目还演示了模型-视图-控制器模式的使用

    命令模式示例项目这个项目是命令模式的一个例子。 该项目还演示了模型-视图-控制器模式的使用。目标以某种形式使用命令模式在这个项目中; 命令模式用于模拟绘制程序。 重做示例由于项目模拟了一个绘制程序,每个...

    研磨设计模式(完整带书签).part1.pdf

    第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式...

    研磨设计模式-part2

    第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式...

    源码:阎宏设计模式光盘

    com.javapatterns.command 命令模式 com.javapatterns.command.swingundo 专题:Swing库中的命令撤销和恢复 com.javapatterns.composite 合成模式 com.javapatterns.decorator 装饰模式 com.javapatterns.dip ...

    研磨设计模式.part2(共4个)

    第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式...

    研磨设计模式-part4

    第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式...

    研磨设计模式-part3

    第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式...

    深入浅出设计模式(中文版)

    5.2CommandPattern(命令模式) 179 5.2.1定义 179 5.2.2现实中的实例——餐馆订菜 180 5.2.3C#实例——简单计算器 181 5.2.4Java实例——总开关 185 5.2.5优势和缺陷 189 5.2.6应用情景 189 5.3Interpreter...

    研磨设计模式.part3(共4个)

    第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式...

    研磨设计模式.part4(共4个)

    第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式...

    研磨设计模式.part1(共4个)

    第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式...

    深入浅出设计模式(中文版电子版)

    5.2CommandPattern(命令模式) 179 5.2.1定义 179 5.2.2现实中的实例——餐馆订菜 180 5.2.3C#实例——简单计算器 181 5.2.4Java实例——总开关 185 5.2.5优势和缺陷 189 5.2.6应用情景 189 5.3Interpreter...

    TCL编程初步

    例 1-2 source命令的简单例子 3 例 2-1 输出一个词的例子 5 例 2-2 参数定义、赋值与参数值引用 5 例 2-3 嵌套$用做替换操作. 6 例 2-4 命令替换[]的例子 6 例 2-5 {}替换的例子.. 6 例 2-6 \的例子.7 例 2-7...

Global site tag (gtag.js) - Google Analytics