用C#改写Head First Design PatternsObserver观察者(原创)

观察者,被观察实时数据推送给观察者

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

namespace Observer
{
    interface Subject
    {
         void register(Observer o);
         void remove(Observer o);
         void notify();
    }

    interface Observer
    {
         void update(float temp);
    }

    interface Dispaly
    {
         void display();
    }
}

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


namespace Observer
{

    /// <summary>
    /// 可被观察的数据,里面有观察者列表
    /// </summary>
    class DataClass : Subject
    {
        //观察者列表
        private List<Observer> observers;

        //被实时观察的温度
        private float temp;

        public DataClass()
        {
            observers = new List<Observer>();
        }

      
        //添加观察者
        public void register(Observer o)
        {
            observers.Add(o);
        }

        //删除观察者
        public void remove(Observer o)
        {
            //或者:
            //
            //int i = observers.IndexOf(o);
            //if (i > 0)
            //{
            //    observers.RemoveAt(i);
            //}

            observers.Remove(o);

        }

        //提醒观察者进行数据更新
        public void notify()
        {
            for (int i = 0; i < observers.Count; i++)
            {
                Observer o = (Observer)observers[i];
                o.update(temp);
            }
        }

        /// <summary>
        /// 改变数值,并提醒观察者进行数据更新
        /// </summary>
        public void setValue(float temp)
        {
            this.temp = temp;
            notify();

        }
    }
}

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

namespace Observer
{
    /// <summary>
    /// 观察者之一
    /// </summary>
    class FirstDisplay : Observer,Dispaly
    {
        private float temp;

        //被观察的数据作为成员变量
        private Subject subjectData;

        public FirstDisplay(Subject s)
        {
            this.subjectData = s;

            //最重要的一句,为可被观察数据注册观察者
            subjectData.register(this);
        }

        public void update(float temp)
        {
            this.temp = temp;
            display();
        }

        /// <summary>
        /// 观察者之一的显示方式
        /// </summary>
        public void display()
        {
            System.Console.Out.WriteLine("当前数值:" + temp);
        }


    }
}

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

namespace Observer
{
    class OtherDisplay:Observer,Dispaly
    {
        private float temp;
        private Subject subjectData;

        public OtherDisplay(Subject s)
        {
            this.subjectData = s;
            subjectData.register(this);
        }

        public void update(float temp)
        {
            this.temp = temp;
            display();
        }

        public void display()
        {
            System.Console.Out.WriteLine("当前数值指数:" + temp*2);
        }


    }
}

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

namespace Observer
{
    class Program
    {
        static void Main(string[] args)
        {
            DataClass data = new DataClass();
            FirstDisplay f = new FirstDisplay(data);
            OtherDisplay o = new OtherDisplay(data);

            data.setValue(80);
            data.setValue(99);

            System.Console.ReadLine();
        }
    }
}

关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
目前维护的开源产品:https://gitee.com/475660
原文地址:https://www.cnblogs.com/starcrm/p/1519226.html