观察者

    public delegate void CatCall();
public class Cat : subject
{
public void cry()
{
Console.WriteLine("猫叫了~");
this.fireAway();
}
}
public class subject
{
public event CatCall myEnvet;
protected void fireAway()
{
if (this.myEnvet != null)
{
this.myEnvet();
}
}
}
public abstract class Observer
{
public Observer(subject sub)
{
sub.myEnvet += new CatCall(Response);
}
public abstract void Response();
}
public class Mouse : Observer
{
private string MouseName;
public Mouse(subject sub, string name)
: base(sub)
{
this.MouseName = name;
}
public override void Response()
{
Console.WriteLine(MouseName + "快跑,喵星人来了!");
}
}
public class H : Observer
{
public H(subject sub)
: base(sub)
{

}
public override void Response()
{
Console.WriteLine("老鼠把我吵醒了!");
}
}
public class dos
{
static void Main()
{
Cat cat = new Cat();
Mouse mouse1 = new Mouse(cat, "Tom");
Mouse mouse2 = new Mouse(cat, "jack");
H h = new H(cat);
cat.cry();
Console.ReadKey();
}
}
posted @ 2012-03-07 14:28  Rookier  阅读(216)  评论(0编辑  收藏  举报