using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Cat cat = new Cat();
Mouse mouse1 = new Mouse("小老鼠", cat);
Mouse mouse2 = new Mouse("大老鼠", cat);
Master master = new Master(cat);
cat.Cry();
}
}
public delegate void SubjectEventHandle();
public abstract class Subject
{
public event SubjectEventHandle subEvent;
protected void FireAway()
{
if (this.subEvent != null)
{
this.subEvent();
}
}
}
public class Cat : Subject
{
public void Cry()
{
HttpContext.Current.Response.Write("猫大叫一声。");
this.FireAway();
}
}
public abstract class Observer
{
public Observer(Subject sub)
{
sub.subEvent += new SubjectEventHandle(sub_subEvent);
}
public abstract void sub_subEvent();
}
public class Mouse : Observer
{
private string name;
public Mouse(string name, Subject sub)
: base(sub)
{
this.name = name;
}
public override void sub_subEvent()
{
HttpContext.Current.Response.Write(this.name + "赶紧跑了。");
}
}
public class Master : Observer
{
public Master(Subject sub)
: base(sub)
{
}
public override void sub_subEvent()
{
HttpContext.Current.Response.Write("主人被吵醒了。");
}
}
}