昨天看到一个简单的面试题:
猫尖叫,主人醒了,老鼠跑了。题目要求实现具体算法。 好在工作暂时不太忙,大致写写玩。
其实一看题目,大家基本都认定这就是观察着模式的实现,没错!具体实现起来,我考虑有两种方法。第一是用委托实现,第二种是不用委托实现。
不管用不用委托,我们先分析一下题目。题目中有个因果关系也就是 猫的尖叫,触发了主人的醒和老鼠的逃跑。这样我们就把对象分为两类:一类是事件源,比如猫,一类是对事件源的某些动作有响应的其他对象比如主人和老鼠。这样对象就抽象出来了。另外的话就是对象的行为,根据猫和主人,老鼠行为的触发关系,我们可以抽象出事件源的行为方法,响应者具体有反应方法,这样我们就可以先定义两个接口,一个是事件源的行为方法,另外一个是响应者的反应方法,这样具体的类可以继承接口,实现彼此的响应关系:
事件源
public interface ISubject
{
void Register(IObserver o);
void Remove(IObserver o);
void Action();
}
public delegate void EventHandleS(object sender,System.EventArgs e);
public class Subject : ISubject
{
private List<IObserver> list = new List<IObserver>();
private int m_Key;
public int Key
{
set
{
m_Key = value;
Action();
}
}
ISubject 成员#region ISubject 成员
public void Register(IObserver o)
{
list.Add(o);
}
public void Remove(IObserver o)
{
list.Remove(o);
}
#endregion
ISubject 成员#region ISubject 成员
public void Action()
{
System.Console.WriteLine(" Cat scraming!!!!");
foreach (IObserver o in list)
{
o.DoSomeThing(this,null);
}
}
#endregion
}
public class SubectByEvent : ISubject
{
private int m_key;
/**//// <summary>
/// 修改关键字 触发订阅端
/// </summary>
public int Key
{
set
{
m_key = value;
Action();
}
}
ISubject 成员#region ISubject 成员
private event EventHandleS NoticeHandle;
public event EventHandleS PNoticeHandle
{
add
{
NoticeHandle = (EventHandleS)Delegate.Combine(NoticeHandle, value);
}
remove
{
NoticeHandle = (EventHandleS)Delegate.Remove(NoticeHandle, value);
}
}
public void Register(IObserver o)
{
NoticeHandle += new EventHandleS(o.DoSomeThing);
}
public void Remove(IObserver o)
{
NoticeHandle -= new EventHandleS(o.DoSomeThing);
}
public void Action()
{
if (NoticeHandle != null)
{
System.Console.WriteLine("CAT IS SCARMING");
NoticeHandle.Invoke(this, null);
}
}
#endregion
}
public class EventHandleSet : IDisposable
{
private System.Collections.Hashtable Handles;
public EventHandleSet()
{
Handles = new System.Collections.Hashtable();
}
public void Add(object key, System.EventHandler handle)
{
if (Handles.Contains(key))
{
}
}
IDisposable 成员#region IDisposable 成员
public void Dispose()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
public interface ISubject
{
void Register(IObserver o);
void Remove(IObserver o);
void Action();
}
public delegate void EventHandleS(object sender,System.EventArgs e);
public class Subject : ISubject
{
private List<IObserver> list = new List<IObserver>();
private int m_Key;
public int Key
{
set
{
m_Key = value;
Action();
}
}
ISubject 成员#region ISubject 成员
public void Register(IObserver o)
{
list.Add(o);
}
public void Remove(IObserver o)
{
list.Remove(o);
}
#endregion
ISubject 成员#region ISubject 成员
public void Action()
{
System.Console.WriteLine(" Cat scraming!!!!");
foreach (IObserver o in list)
{
o.DoSomeThing(this,null);
}
}
#endregion
}
public class SubectByEvent : ISubject
{
private int m_key;
/**//// <summary>
/// 修改关键字 触发订阅端
/// </summary>
public int Key
{
set
{
m_key = value;
Action();
}
}
ISubject 成员#region ISubject 成员
private event EventHandleS NoticeHandle;
public event EventHandleS PNoticeHandle
{
add
{
NoticeHandle = (EventHandleS)Delegate.Combine(NoticeHandle, value);
}
remove
{
NoticeHandle = (EventHandleS)Delegate.Remove(NoticeHandle, value);
}
}
public void Register(IObserver o)
{
NoticeHandle += new EventHandleS(o.DoSomeThing);
}
public void Remove(IObserver o)
{
NoticeHandle -= new EventHandleS(o.DoSomeThing);
}
public void Action()
{
if (NoticeHandle != null)
{
System.Console.WriteLine("CAT IS SCARMING");
NoticeHandle.Invoke(this, null);
}
}
#endregion
}
public class EventHandleSet : IDisposable
{
private System.Collections.Hashtable Handles;
public EventHandleSet()
{
Handles = new System.Collections.Hashtable();
}
public void Add(object key, System.EventHandler handle)
{
if (Handles.Contains(key))
{
}
}
IDisposable 成员#region IDisposable 成员
public void Dispose()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
上面是具体事件源类型的一些实现细节,里面涵盖了两种方式。一种是委托的方式,一种是引用的方式,本质都是相同的。我理解就是 要想让响应类能够对事件源的动作有处理,事件源类应该持有响应类的引用,不管这种引用时通过委托链呢还是自行构造的散列表。上面代码中有两个类继承了事件源的接口,一个是委托方式实现的,一个是引用方式实现的。
下面的响应类的实现:
响应类
public interface IObserver
{
void DoSomeThing(object sender,System.EventArgs e);
}
public abstract class Oberver : IObserver
{
public Oberver(SubectByEvent subject)
{
//subject.Register(this);
subject.PNoticeHandle += new EventHandleS(DoSomeThing);
}
public void CancleMember(SubectByEvent subject)
{
// subject.Remove(this);
subject.PNoticeHandle -= new EventHandleS(DoSomeThing);
}
IObserver 成员#region IObserver 成员
public virtual void DoSomeThing()
{
}
public void DoSomeThing(object sender, System.EventArgs e)
{
DoSomeThing();
}
#endregion
}
public class Host : Oberver
{
public Host(SubectByEvent subject)
: base(subject)
{ }
public override void DoSomeThing()
{
base.DoSomeThing();
System.Console.WriteLine("Host Knows!!");
}
}
public class Mouse : Oberver
{
public Mouse(SubectByEvent subject)
: base(subject)
{ }
/**//// <summary>
/// 重载的时候 除非手动调用基类的函数 否则都以子类实现为主
/// </summary>
public override void DoSomeThing()
{
System.Console.WriteLine("Mouse escape!!");
}
}
public interface IObserver
{
void DoSomeThing(object sender,System.EventArgs e);
}
public abstract class Oberver : IObserver
{
public Oberver(SubectByEvent subject)
{
//subject.Register(this);
subject.PNoticeHandle += new EventHandleS(DoSomeThing);
}
public void CancleMember(SubectByEvent subject)
{
// subject.Remove(this);
subject.PNoticeHandle -= new EventHandleS(DoSomeThing);
}
IObserver 成员#region IObserver 成员
public virtual void DoSomeThing()
{
}
public void DoSomeThing(object sender, System.EventArgs e)
{
DoSomeThing();
}
#endregion
}
public class Host : Oberver
{
public Host(SubectByEvent subject)
: base(subject)
{ }
public override void DoSomeThing()
{
base.DoSomeThing();
System.Console.WriteLine("Host Knows!!");
}
}
public class Mouse : Oberver
{
public Mouse(SubectByEvent subject)
: base(subject)
{ }
/**//// <summary>
/// 重载的时候 除非手动调用基类的函数 否则都以子类实现为主
/// </summary>
public override void DoSomeThing()
{
System.Console.WriteLine("Mouse escape!!");
}
}
响应类大致依赖于事件源类,一般如果事件源有委托的话,就知道挂自己的响应方法,没有委托的话就调用注册函数。我看了一下感觉老鼠和主人的很多东西都是里相似的,干脆再抽出一层,这样写起来舒服点。over!
吃饭去了 呵呵