一、观察者模式
二、命令模式
IObserver
ExecuteNotify(ISubject subject)
ISubject
AddObserver(IObserver observer)
RemoveObserver(IObserver observer)
ObserversCount { get }
ClearObservers()
SetChanged()
HasChanged { get }
NotifyObservers()
{
foreach (IObserver observer in Observers)
{
observer.ExecuteNotify(this);
}
}
ExecuteNotify(ISubject subject)
ISubject
AddObserver(IObserver observer)
RemoveObserver(IObserver observer)
ObserversCount { get }
ClearObservers()
SetChanged()
HasChanged { get }
NotifyObservers()
{
foreach (IObserver observer in Observers)
{
observer.ExecuteNotify(this);
}
}
二、命令模式
public class Command
{
public delegate void EnableChangedEventHandler(object sender, Command.EnableChangedEventArgs e);
public virtual event EnableChangedEventHandler EnableChanged;
public delegate void Action();
private Action m_action;
private bool m_isEnabled = true;
public bool IsEnabled
{
get
{
return m_isEnabled;
}
set
{
if (m_isEnabled != value)
{
m_isEnabled = value;
if (EnableChanged != null)
{
EnableChanged(this, new EnableChangedEventArgs(IsEnabled));
}
}
}
}
public Command(Action action)
{
m_action = action;
}
// Invokes the method assigned to this command.
public void Execute()
{
m_action();
}
// Arguments passed to the EnableChanged event.
public class EnableChangedEventArgs : EventArgs
{
private bool m_isEnabled = false;
public bool IsEnabled
{
get
{
return m_isEnabled;
}
}
public EnableChangedEventArgs(bool isEnabled)
{
m_isEnabled = isEnabled;
}
}
}
{
public delegate void EnableChangedEventHandler(object sender, Command.EnableChangedEventArgs e);
public virtual event EnableChangedEventHandler EnableChanged;
public delegate void Action();
private Action m_action;
private bool m_isEnabled = true;
public bool IsEnabled
{
get
{
return m_isEnabled;
}
set
{
if (m_isEnabled != value)
{
m_isEnabled = value;
if (EnableChanged != null)
{
EnableChanged(this, new EnableChangedEventArgs(IsEnabled));
}
}
}
}
public Command(Action action)
{
m_action = action;
}
// Invokes the method assigned to this command.
public void Execute()
{
m_action();
}
// Arguments passed to the EnableChanged event.
public class EnableChangedEventArgs : EventArgs
{
private bool m_isEnabled = false;
public bool IsEnabled
{
get
{
return m_isEnabled;
}
}
public EnableChangedEventArgs(bool isEnabled)
{
m_isEnabled = isEnabled;
}
}
}
// base Commander class
public abstract class Commander
{
protected Command m_command;
protected abstract void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e);
protected Commander(Command command)
{
m_command = command;
m_command.EnableChanged += new Command.EnableChangedEventHandler(this.HandleEnableChangedEvent);
}
}
// MenuItemCommander class
public class MenuItemCommander : Commander
{
private MenuItem m_item;
protected MenuItemCommander(MenuItem item, Command command) : base(command)
{
m_item = item;
m_item.Click += new EventHandler(this.HandleUIEvent);
}
protected override void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)
{
m_item.Enabled = e.IsEnabled;
}
private void HandleUIEvent(object sender, EventArgs e)
{
m_command.Execute();
}
// Connect is a shared (static) method that performs the task of adapting a menu
// item to a command. The commander exists only to wire up the two objects --
// it is not used further
public static void Connect(MenuItem item, Command command)
{
MenuItemCommander unused = new MenuItemCommander(item, command);
}
}
public abstract class Commander
{
protected Command m_command;
protected abstract void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e);
protected Commander(Command command)
{
m_command = command;
m_command.EnableChanged += new Command.EnableChangedEventHandler(this.HandleEnableChangedEvent);
}
}
// MenuItemCommander class
public class MenuItemCommander : Commander
{
private MenuItem m_item;
protected MenuItemCommander(MenuItem item, Command command) : base(command)
{
m_item = item;
m_item.Click += new EventHandler(this.HandleUIEvent);
}
protected override void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)
{
m_item.Enabled = e.IsEnabled;
}
private void HandleUIEvent(object sender, EventArgs e)
{
m_command.Execute();
}
// Connect is a shared (static) method that performs the task of adapting a menu
// item to a command. The commander exists only to wire up the two objects --
// it is not used further
public static void Connect(MenuItem item, Command command)
{
MenuItemCommander unused = new MenuItemCommander(item, command);
}
}