观察者模式示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyOb
{
    public delegate void ObserverDelegate<T>(T e);
    public class Observer<T>
    {
        // 利用委托对象做为 观察者列表

        //函数指针
        private ObserverDelegate<T> observerList;

        //事件

        //注册或移除观察者
        public event ObserverDelegate<T> Observers
        {
            add //注册观察者
            {
                //指针增加绑定函数
                observerList += value;
            }
            remove//移除观察者
            {
                observerList -= value;
            }
        }

        protected virtual void OnHandler(T e)
        {
            if (observerList != null)
                observerList(e);
        }

        //通知所有观察者 主题的状态发生改变
        protected void Notify(T e)
        {
            OnHandler(e);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyOb
{
    interface ICommand
    {
        void Execute();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyOb
{
    public class Sample4 : ICommand
    {
        protected class WaterMessage
        {
            public readonly Heater Heater;
            public readonly int Temperature;

            public WaterMessage(Heater heater, int temperature)
            {
                this.Heater = heater;
                this.Temperature = temperature;
            }
        }

        protected class Heater : Observer<WaterMessage>
        {
            public string Type = "001";        // 添加型号作为演示
            public string Area = "China";            // 添加产地作为演示

            public void BoildWater()
            {
                for (int temperature = 0; temperature <= 100; temperature++)
                {
                    if (temperature > 98)
                    {
                        Notify(new WaterMessage(this, temperature));
                    }
                }
            }


        }
        public  void Execute()
        {
            // 主题对象 - 热水器
            Heater heater = new Heater();

            //注册观察者 - 警报器 ,事件绑定函数
            heater.Observers += MakeAlert;
            //注册观察者 - 显示器 ,事件绑定函数
            heater.Observers += ShowMsg;
            heater.BoildWater();

            Lighter lighter = new Lighter();

            //张三进屋看到了一个大灯
            lighter.Observers += ZhangSan;
            //李四进屋看到了一个大灯
            lighter.Observers += LiSi;


            lighter.Opened = true;
            lighter.Opened = false;
            lighter.Opened = true;
        }

        //现在的消息处理函数可是泛型的了呀,不需要类型转化了
        private void MakeAlert(WaterMessage m)
        {
            Console.WriteLine("Alarm:{0} - {1}: ", m.Heater.Area, m.Heater.Type);
            Console.WriteLine("Alarm: 嘀嘀嘀,水已经 {0} 度了:", m.Temperature.ToString());
            Console.WriteLine();
        }

        //现在的消息处理函数可是泛型的了呀,不需要类型转化了
        private void ShowMsg(WaterMessage m)
        {
            Console.WriteLine("Display:{0} - {1}: ", m.Heater.Area, m.Heater.Type);
            Console.WriteLine("Display:水快烧开了,当前温度:{0}度。", m.Temperature.ToString());
            Console.WriteLine();
        }

        private void Response(string person, bool opened)
        {
            if (opened)
            {
                Console.WriteLine("{0} 激动地喊【奥,来电了!】", person);
            }
            else
            {
                Console.WriteLine("{0} 叹气的说【娘的,又停电了!】", person);
            }
        }

        public void ZhangSan(bool e)
        {
            Response("ZhangSan", (bool)e);
        }

        public void LiSi(bool e)
        {
            Response("LiSi", e);
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyOb
{
    class Lighter : Observer<bool>
    {
        private bool opened = false;
        public bool Opened
        {
            get { return opened; }
            set
            {
                if (opened != value)
                {
                    opened = value;
                    Notify(value);
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyOb
{
    class Program
    {
        static void Main(string[] args)
        {
            ICommand sample = new Sample4();
            sample.Execute();
            Console.ReadLine();
        }
    }
}

 

posted on 2015-08-21 11:08  米高佐敦  阅读(373)  评论(0编辑  收藏  举报