程序设计_猫老鼠主人

问题


猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。

分析


从这句话,我们可以获得两个信息:

  1. 涉及到三个角色,猫,老鼠和主人。因此,显然要定义三个类Cat,Mouse和Master。至于这三个类之间的关系,看你如何选择实现的方式;
  2. 当猫发出一个动作时,老鼠和主人都会做出自己的反应。因此,第一个能够想到的实现方式是事件委托方式。但是设计模式中,有一个观察者模式,也能够达到这个要求。

解决方案


这样,或是用事件委托方式,或是用观察者模式。

方案一:观察者模式

public interface Observer
{
    void Response();
}
 
public interface Subject
{
    void AimAt(Observer ob);
}
 
public class Cat : Subject
{
    private ArrayList _observers;
    public ArrayList Observers
    {
        get { return this._observers; }
        set { this._observers = value; }
    }
    public Cat()
    {
        this.Observers = new ArrayList();
    }
    public void AimAt(Observer ob)
    {
        this.Observers.Add(ob);
    }
    public void Cryed()
    {
        Console.WriteLine("Now, Cat Cryed!");
        foreach (Observer ob in this.Observers)
        {
            ob.Response();
        }
    }
}
 
public class Mouse : Observer
{
    private string _name;
    public string Name
    {
        get { return this._name; }
        set { this._name = value; }
    }
    public Mouse(string name, Subject su)
    {
        this.Name = name; 
        su.AimAt(this);
    }
    public void Response()
    {
        Console.WriteLine("Then, " + this.Name + " Attempt To Escape!");
    }
}
 
public class Master : Observer
{
    public Master(Subject su)
    {
        su.AimAt(this);
    }
    public void Response()
    {
        Console.WriteLine("Then, Host Waken Up!");
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        Cat cat = new Cat();
        Mouse m1 = new Mouse("m1", cat);
        Mouse m2 = new Mouse("m2", cat);
        Master h = new Master(cat);
        cat.Cryed();
        Console.ReadKey();
    }
}

方案二:事件委托

public delegate void SubEventHandler();
 
public abstract class Subject
{
    public event SubEventHandler subEvent;
    protected void OnFireAwayEventHandler()
    {
        if (this.subEvent != null)
        {
            this.subEvent();
        }
    }
}
 
public class Cat : Subject
{
    public void Cryed()
    {
        Console.WriteLine("Now, Cat Cryed!");
        this.OnFireAwayEventHandler();
    }
}
 
public abstract class Observer
{
    public Observer(Subject su)
    {
        su.subEvent += new SubEventHandler(Response);
    }
    public abstract void Response();
}
 
public class Mouse : Observer
{
    private string _name;
    public string Name
    {
        get { return this._name; }
        set { this._name = value; }
    }
    public Mouse(string name, Subject su)
        : base(su)
    {
        this.Name = name;
    }
    public override void Response()
    {
        Console.WriteLine("Then, " + this.Name + " Attempt To Escape!");
    }
}
 
public class Master : Observer
{
    public Master(Subject su)
        : base(su)
    { }
    public override void Response()
    {
        Console.WriteLine("Then, Host Waken Up!");
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        Cat cat = new Cat();
        Mouse m1 = new Mouse("m1", cat);
        Mouse m2 = new Mouse("m2", cat);
        Master h = new Master(cat);
        cat.Cryed();
        Console.ReadKey();
    }
}

都会得到相同的结果:

Now, Cat Cryed!
Then, m1 Attempt To E
Then, m2 Attempt To E
Then, Host Waken Up!

 

下载 Demo

posted @ 2011-09-01 22:09  船长&CAP  阅读(357)  评论(1编辑  收藏  举报
免费流量统计软件