管理者一个小例子,来自软件小程序设计
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
public class Application
{
static void Main(string[] args)
{
CommandMangers command = CommandMangers.Instance;
command.AddCommand( "Open", new OpenCommand());
command.AddCommand( "Close", new CloseCommand());
command.ExcuteCommad("Close");
Console.Read();
}
}
public class CommandBase{
public virtual void Excute() { }
}
class OpenCommand : CommandBase
{
public override void Excute()
{
Console.Write("打开空间");
}
}
class CloseCommand : CommandBase
{
public override void Excute()
{
Console.WriteLine("关闭空间");
}
}
public class CommandMangers {
public static readonly CommandMangers Instance= new CommandMangers();
private CommandMangers()
{
}
Dictionary<string, CommandBase> DicCommand = new Dictionary<string, CommandBase>();
public void AddCommand(string name,CommandBase command )
{
DicCommand.Add(name, command);
}
public void ExcuteCommad(string commandName)
{
foreach (var item in DicCommand)
{
if (item.Key==commandName)
{
item.Value.Excute();
}
}
}
}
}