[设计模式] 15.Command 命令模式

我的理解:
命令池(采用堆或栈皆可)维护着一组命令集合。
只要这些命令实现同个命令接口或者命令抽象类,就能够被命令池依次执行。

None.gif class  App
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//命令模式:将无论哪个实例的方法抽象成对应的命令放入命令池。命令池会自动执行。
InBlock.gif
        Printer printer = new Printer();
InBlock.gif        Scanner scanner 
= new Scanner();
InBlock.gif        PrintCommand pc 
= new PrintCommand(printer);
InBlock.gif        ScanCommand sc 
= new ScanCommand(scanner);
InBlock.gif        CommandQueue.Instance.Enquee(pc);
InBlock.gif        CommandQueue.Instance.Enquee(sc);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif // ICommand命令池,计时器每过一段时间取出一个命令执行
None.gif
public   class  CommandQueue : Queue < ICommand >
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif    
单例#region 单例
InBlock.gif    
private static class CommandQueueInstance
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static readonly CommandQueue Instance = new CommandQueue();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static CommandQueue Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return CommandQueueInstance.Instance;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif    
InBlock.gif    
private Timer _t;
InBlock.gif
InBlock.gif    
private CommandQueu()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        _t 
= new Timer(new TimerCallback(Execute), null5000200);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private void Execute(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (this.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ICommand cmd 
= this.Dequeue();
InBlock.gif            cmd.Execute();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif // 所有命令的抽象,这里使用接口比较好
None.gif
public   interface  ICommand
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
void Execute();
ExpandedBlockEnd.gif}

None.gif // 打印机打印的命令
None.gif
public   class  PrintCommand : ICommand
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private Printer _printer;
InBlock.gif    
public PrintCommand( Printer printer)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        _printer 
= printer;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public virtual void Execute()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        _printer.Print();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif // 扫描仪扫描的命令
None.gif
public   class  ScanCommand : ICommand
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private Scanner _scanner;
InBlock.gif    
public ScanCommand(Scanner scanner)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        _scanner 
= scanner;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public virtual void Execute()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        _scanner.Scan();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif // 打印机提供打印方法
None.gif
public   class  Printer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public void Print()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(
"Print.");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif // 扫描仪提供扫描方法
None.gif
public   class  Scanner
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public void Scan()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(
"Scan.");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/yurichou/archive/2007/08/13/854151.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值