观察者模式之Tom and Jerry

namespace TomAndJerry
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 通过接口来完成
            ////猫
            //Tom t = new Tom();
            ////Mouse
            //Jerry j = new Jerry(t);
            //Jerry j1 = new Jerry(t);
            ////Owner
            //Owner o = new Owner(t);

            //t.Cry();
            //Console.Read();
            #endregion

            #region 通过事件来完成

            //Cat cat = new Cat("Tom");
            ////建立关系
            //Mouse jerry = new Mouse("Jerry", cat);

            //Owner o = new Owner("Jason", cat);

            ////Tom 触发事件
            //cat.CatCry();

            //Console.Read();

            #endregion

            #region 通过抽象类完成

            Cat cat = new Cat();
            Mouse jerr = new Mouse(cat);
            Owner o = new Owner(cat);

            cat.Cry();
            Console.Read();

            #endregion

        }
    }

    #region 通过接口完成

    ////定义主动者接口
    //public interface IInitiative
    //{
    //    //通知的方法,要与接受者建立关系
    //    void Notice(IReceivenotice irnotice);
    //}

    ////定义接受者接口
    //public interface IReceivenotice
    //{
    //    //有一个表现的方法
    //    void Show();
    //}

    ////定义Tom 类,Tom 是主动者,继承主动者接口, 由它发出并触发相应的事件
    //public class Tom : IInitiative
    //{
    //    //由于发出的主动者不确定,所以放在一个数组中
    //    ArrayList alist = new ArrayList();

    //    #region IInitiative 成员

    //    public void Notice(IReceivenotice irnotice)
    //    {
    //        //建立起通知关系
    //        alist.Add(irnotice);
    //    }

    //    //猫叫的事件
    //    public void Cry()
    //    {
    //        Console.WriteLine("Tom is crying");
    //        //主动者发出的通知,接受者做出的反应都放到alist中,调用接受者的反应
    //        foreach (IReceivenotice notice in alist)
    //        {
               
    //            notice.Show();
               
    //        }
    //    }

    //    #endregion
    //}

    ////定义Jerry 类, Jerry 是接受者,继承接受者的接口
    //public class Jerry : IReceivenotice
    //{
    //    public Jerry()
    //    { }

    //    //明确是谁来通知的,一般有接口的父类来通知
    //    public Jerry(IInitiative iiative)
    //    {
    //        //this 表示此接口的实例
    //        iiative.Notice(this);
    //    }

    //    #region IReceivenotice 成员

    //    public void Show()
    //    {
    //        //收到通知后,产生活动
    //        Console.WriteLine("Jerry is running crazy!");
    //    }

    //    #endregion
    //}

    ////定义Owner 类, Owner 是接受者,继承接受者的接口
    //public class Owner : IReceivenotice
    //{
    //    public Owner()
    //    { }

    //    //明确是谁来通知的,一般有接口的父类来通知
    //    public Owner(IInitiative iiative)
    //    {
    //        iiative.Notice(this);
    //    }

    //    #region IReceivenotice 成员

    //    public void Show()
    //    {
    //        ////收到通知后,产生活动
    //        Console.WriteLine("The owner is wakeup , cry :'Tom , what are you doing ?'");
    //    }

    //    #endregion
    //}

    #endregion

    #region 通过事件完成

    #region 猫
    //public class Cat
    //{
    //    //猫的名字
    //    private string _name;

    //    //猫叫的事件
    //    public event EventHandler<CatCryEventArgs> CatCryEvent;

    //    //构造函数
    //    public Cat()
    //    { }
    //    public Cat(string name)
    //    {
    //        this._name = name;
    //    }

    //    //触发猫叫的事件
    //    public void CatCry()
    //    {
    //        CatCryEventArgs args = new CatCryEventArgs(_name);
    //        Console.WriteLine(args);
    //        CatCryEvent(this, args);
    //    }
    //}
    #endregion

    #region 猫叫的事件

    //public class CatCryEventArgs : EventArgs
    //{
    //    //发出叫声的猫的名字
    //    private string _name;
    //    //构造函数
    //    public CatCryEventArgs()
    //    { }
    //    public CatCryEventArgs(string name)
    //    {
    //        this._name = name;
    //    }

    //    //输出参数的内容
    //    public override string ToString()
    //    {
    //        return _name + " crying !";
    //    }
    //}

    #endregion
   
    #region 主人

    //public class Owner
    //{
    //    //主人的名
    //    private string name;

    //    //构造函数
    //    public Owner()
    //    { }
    //    public Owner(string _name,Cat cat)
    //    {
    //        this.name = _name;
    //        cat.CatCryEvent += new EventHandler<CatCryEventArgs>(CatCryEventHandle);
    //    }

    //    //猫叫的事件的处理方法
    //    void CatCryEventHandle(object sender, CatCryEventArgs e)
    //    {
    //        WakeUp();
    //    }

    //    //主人被惊醒了的方法
    //    private void WakeUp()
    //    {
    //        Console.WriteLine(name + " is wakeup and cry 'Tom what are you doing ?'");
    //    }
    //}

    #endregion

    #region 老鼠

    //public class Mouse
    //{
    //    //老鼠名
    //    private string _name;

    //    //构造函数
    //    public Mouse()
    //    { }

    //    public Mouse(string name, Cat cat)
    //    {
    //        this._name = name;
    //        cat.CatCryEvent += new EventHandler<CatCryEventArgs>(CatCryHandleRun);
    //    }

    //    //猫叫事件的处理方法
    //    void CatCryHandleRun(object sender, CatCryEventArgs e)
    //    {
    //        Run();
    //    }

    //    //老鼠跑的方法
    //    private void Run()
    //    {
    //        Console.WriteLine(_name + " is running !");
    //    }
    //}

    #endregion

    #endregion

    #region 通过抽象类完成,与接口完成类似

    //定义主动
    public abstract class Observer
    {
        //通知的方法
        public abstract void Notice(Reobj robj);
    }

    //定义接收
    public abstract class Reobj
    {
        public abstract void Show();
    }

    //定义主动者
    public class Cat : Observer
    {
        ArrayList al = new ArrayList();

        public override void Notice(Reobj robj)
        {
            al.Add(robj);
        }

        public void Cry()
        {
            Console.WriteLine("Tom 大喊一声!");
            foreach (Reobj o in al)
            {
                o.Show();
            }
        }
    }

    //定义接收者
    public class Mouse : Reobj
    {
        public Mouse()
        { }
        public Mouse(Observer obs)
        {
            obs.Notice(this);
        }

        public override void Show()
        {
            Console.WriteLine("Jerry is running!");
        }
    }

    //定义Owner
    public class Owner : Reobj
    {
        public Owner()
        { }

        public Owner(Observer obs)
        {
            obs.Notice(this);
        }  

        public override void Show()
        {
            Console.WriteLine("主人醒了,大喊:Tom what are you doing?");
        }
    }

    #endregion
}

原文地址:https://www.cnblogs.com/jasonjiang/p/1763448.html