using System;
namespace Cat
{
public delegate void SubEventHandler(object sender,EventArgs e);
public class Subject
{
public event SubEventHandler subEvent;
public virtual void OnAction(EventArgs e)
{
if (subEvent != null)
{
subEvent(this, e);
}
}
}
public abstract class Observer
{
private Subject sub;
public Observer(Subject sub)
{
this.sub = sub;
this.sub.subEvent += new SubEventHandler(ObResult);
}
public abstract void ObResult(object sender, EventArgs e);
}
class Cat:Subject
{
public void Scream()
{
Console.WriteLine("猫叫~~");
OnAction(EventArgs.Empty);
}
}
class Mouse : Observer
{
public Mouse(Subject sub)
: base(sub)
{
}
public override void ObResult(object sender, EventArgs e)
{
Console.WriteLine("老鼠吓跑了~");
}
}
class Master : Observer
{
public Master(Subject sub)
: base(sub)
{
}
public override void ObResult(object sender, EventArgs e)
{
Console.WriteLine("主人醒了~");
}
}
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat();
Mouse[] mouses = new Mouse[3];
for (int i = 0; i < mouses.Length; i++)
{
mouses[i] = new Mouse(cat);
}
Master master = new Master(cat);
cat.Scream();
Console.ReadKey();
}
}
}
namespace Cat
{
public delegate void SubEventHandler(object sender,EventArgs e);
public class Subject
{
public event SubEventHandler subEvent;
public virtual void OnAction(EventArgs e)
{
if (subEvent != null)
{
subEvent(this, e);
}
}
}
public abstract class Observer
{
private Subject sub;
public Observer(Subject sub)
{
this.sub = sub;
this.sub.subEvent += new SubEventHandler(ObResult);
}
public abstract void ObResult(object sender, EventArgs e);
}
class Cat:Subject
{
public void Scream()
{
Console.WriteLine("猫叫~~");
OnAction(EventArgs.Empty);
}
}
class Mouse : Observer
{
public Mouse(Subject sub)
: base(sub)
{
}
public override void ObResult(object sender, EventArgs e)
{
Console.WriteLine("老鼠吓跑了~");
}
}
class Master : Observer
{
public Master(Subject sub)
: base(sub)
{
}
public override void ObResult(object sender, EventArgs e)
{
Console.WriteLine("主人醒了~");
}
}
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat();
Mouse[] mouses = new Mouse[3];
for (int i = 0; i < mouses.Length; i++)
{
mouses[i] = new Mouse(cat);
}
Master master = new Master(cat);
cat.Scream();
Console.ReadKey();
}
}
}