一个command模式的例子

博客展示了Command模式的一个例子,以录音机为场景。定义了录音机对象AudioPlayer作为Receiver,抽象命令类AbstractCommand及具体的播放、停止、倒带命令类,还有充当Invoker的录音机键盘对象KeyPad,最后演示了通过KeyPad执行命令的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

命令模式是对命令的封装.命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象.该模式的示意类图如下:
command.jpg
下面是一个Command模式的例子(模仿<Java与模式>中的例子).
现有一个录音机(AudioPlayer),他有播放(Play),到带(Rewind)和停止(Stop)三个功能,利用Command模式实现中,录音机(AudioPlayer)则为Receiver对象,将录音机的键盘作为Invoker对象,因为客户是通过他去执行播放,倒带,停止的功能(命令).
首先:定义录音机对象
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif    
/// 录音机对象,该对象充当Receiver角色
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class AudioPlayer
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public AudioPlayer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 播放
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Play()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Playingdot.gif");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 停止
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Stop()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Stopingdot.gif");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 倒带
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Rewind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Rewindingdot.gif");
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
接下来定义命令对象,在这里,我采用一个抽象类AbstractCommand(ICommand角色)做为所有具体命令类的父类,所有具体命令类都要继承自该抽象类:
None.gifpublic abstract class AbstractCommand
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
protected AudioPlayer _player;
InBlock.gif        
public AbstractCommand(AudioPlayer player)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._player = player;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public abstract void Execute();
ExpandedBlockEnd.gif    }
具体命令对象如下:
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif    
/// 播放命令
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class PlayCommand : AbstractCommand
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public PlayCommand(AudioPlayer player) : base(player)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
ICommand 成员#region ICommand 成员
InBlock.gif
InBlock.gif        
public override void Execute()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _player.Play();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**//// <summary>
InBlock.gif    
/// 停止命令
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class StopCommand : AbstractCommand
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public StopCommand(AudioPlayer player) : base(player)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
ICommand 成员#region ICommand 成员
InBlock.gif
InBlock.gif        
public override void Execute()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _player.Stop();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**//// <summary>
InBlock.gif    
/// 倒带命令
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class RewindCommand : AbstractCommand
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public RewindCommand(AudioPlayer player) : base(player)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
ICommand 成员#region ICommand 成员
InBlock.gif
InBlock.gif        
public override void Execute()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _player.Rewind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }
接下来定义键盘对象KeyPad(Invoker角色):
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif    
/// 录音机键盘对象,该对象充当Invoker角色
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class KeyPad
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private AbstractCommand _playCmd;
InBlock.gif        
private AbstractCommand _stopCmd;
InBlock.gif        
private AbstractCommand _rewindCmd;
InBlock.gif
InBlock.gif        
public KeyPad(AbstractCommand playCmd,AbstractCommand stopCmd,AbstractCommand rewindCmd)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._playCmd = playCmd;
InBlock.gif            
this._stopCmd = stopCmd;
InBlock.gif            
this._rewindCmd = rewindCmd;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Play()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _playCmd.Execute();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Stop()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _stopCmd.Execute();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Rewind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _rewindCmd.Execute();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
现在,客户端就可以这样控制录音机了:
None.gifAudioPlayer player = new AudioPlayer(); //定义receiver对象
None.gif

ExpandedBlockStart.gifContractedBlock.gif            
/**//*各个具体命令对象*/
None.gif            AbstractCommand playCmd 
= new PlayCommand(player);
None.gif            AbstractCommand stopCmd 
= new StopCommand(player);
None.gif            AbstractCommand rewindCmd 
= new RewindCommand(player);
None.gif
None.gif            KeyPad pad 
= new KeyPad(playCmd,stopCmd,rewindCmd); //定义invoker对象
ExpandedBlockStart.gifContractedBlock.gif
            /**//*通过invoker进行命令的执行*/
None.gif            pad.Play();
None.gif            pad.Rewind();
None.gif            pad.Stop();

转载于:https://www.cnblogs.com/DotNetFresh/archive/2005/06/29/183387.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值