程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#設計模式之Command設計模式(命令設計模式)(2)

C#設計模式之Command設計模式(命令設計模式)(2)

編輯:關於C語言

三、命令模式的示意性源代碼

// Command pattern -- Structural example 
using System;
// "Command"
abstract class Command
{
 // FIElds
 protected Receiver receiver;
 // Constructors
 public Command( Receiver receiver )
 {
  this.receiver = receiver;
 }
 // Methods
 abstract public void Execute();
}
// "ConcreteCommand"
class ConcreteCommand : Command
{
 // Constructors
 public ConcreteCommand( Receiver receiver ) :
  base ( receiver ) {}
 // Methods
 public override void Execute()
 {
  receiver.Action();
 }
}
// "Receiver"
class Receiver
{
 // Methods
 public void Action()
 {
  Console.WriteLine("Called Receiver.Action()");
 }
}
// "Invoker"
class Invoker
{
 // FIElds
 private Command command;
 // Methods
 public void SetCommand( Command command )
 {
  this.command = command;
 }
 public void ExecuteCommand()
 {
  command.Execute();
 }
}
/**//// <summary>
/// ClIEnt test
/// </summary>
public class ClIEnt
{
 public static void Main( string[] args )
 {
  // Create receiver, command, and invoker
  Receiver r = new Receiver();
  Command c = new ConcreteCommand( r );
  Invoker i = new Invoker();
  // Set and execute command
  i.SetCommand(c);
  i.ExecuteCommand();
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved