观察者模式-HeadFirst学习

1、观察者模式定义:

定义对象之间的一对多依赖,如此,一个对象的状态改变时,他的所有依赖者都会收到通知,并自动更新;

2、关键点

该模式定义了两个对象之间的松耦合关系,观察者不知被观察者的细节,被观察者不必知道观察者的具体表现,只要双方交互的接口未变,在扩展性的观察者时,被观察者不必修改代码;

3、例子源码:

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

namespace ObserverPattern
{
    public interface Observer
    {
        void update(string tem,string hum,string pre);
    }

    public interface DisplayElement
    {
        void display();
    }

    public interface Subject
    {
        void registerObserver(Observer observer);

        void removeRegister(Observer observer);

        void notifyObservers();
    }


    public class WeatherData : Subject
    {

        public WeatherData()
        {
            vecObservers = new List<Observer>();
        }

        #region Subject 成员

        public void registerObserver(Observer observer)
        {
            vecObservers.Add(observer);
        }

        public void removeRegister(Observer observer)
        {
            vecObservers.Remove(observer);
        }

        public void notifyObservers()
        {
            foreach (Observer observer in vecObservers)
            {
                observer.update(m_tem, m_hum, m_pre);
            }
        }

        #endregion


        public void SetMeasureMent(string tem, string hum, string pre)
        {
            m_tem = tem;
            m_hum = hum;
            m_pre = pre;

            notifyObservers();
        }

        private string m_tem;
        private string m_hum;
        private string m_pre;

        private List<Observer> vecObservers;
    }

    public class CurrentConditionDisplay : Observer, DisplayElement
    {
        public CurrentConditionDisplay(Subject pSubject)
        {
            m_Subject = pSubject;
            m_Subject.registerObserver(this);
        }

        #region Observer 成员

        public void update(string tem, string hum, string pre)
        {
            m_tem = tem;
            m_hum = hum;
            m_pre = pre;

            display();
        }

        #endregion

        #region DisplayElement 成员

        public void display()
        {
            Console.WriteLine(string.Format("当前显示牌显示状态,{0},{1},{2}", m_tem, m_hum, m_pre));
        }

        #endregion

        private string m_tem;
        private string m_hum;
        private string m_pre;

        private Subject m_Subject;
    }

    public class StatisticsConditionDisplay : Observer, DisplayElement
    {
        public StatisticsConditionDisplay(Subject pSubject)
        {
            m_Subject = pSubject;
            m_Subject.registerObserver(this);
        }

        #region Observer 成员

        public void update(string tem, string hum, string pre)
        {
            m_tem = tem;
            m_hum = hum;
            m_pre = pre;

            display();
        }

        #endregion

        #region DisplayElement 成员

        public void display()
        {
            Console.WriteLine(string.Format("统计显示牌显示状态,{0},{1},{2}", m_tem, m_hum, m_pre));
        }

        #endregion

        private string m_tem;
        private string m_hum;
        private string m_pre;

        private Subject m_Subject;
    }

    public class ForecastConditionDisplay : Observer, DisplayElement
    {
        public ForecastConditionDisplay(Subject pSubject)
        {
            m_Subject = pSubject;
            m_Subject.registerObserver(this);
        }

        #region Observer 成员

        public void update(string tem, string hum, string pre)
        {
            m_tem = tem;
            m_hum = hum;
            m_pre = pre;

            display();
        }

        #endregion

        #region DisplayElement 成员

        public void display()
        {
            Console.WriteLine(string.Format("预报显示牌显示状态,{0},{1},{2}", m_tem, m_hum, m_pre));
        }

        #endregion

        private string m_tem;
        private string m_hum;
        private string m_pre;

        private Subject m_Subject;
    }

}

namespace ObserverPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            WeatherData pSubject = new WeatherData();
            Observer pObserver1 = new CurrentConditionDisplay(pSubject);
            Observer pObserver2 = new StatisticsConditionDisplay(pSubject);
            Observer pObserver3 = new ForecastConditionDisplay(pSubject);
            pSubject.SetMeasureMent("100", "100", "100");
            pSubject.SetMeasureMent("100", "200", "300");
            pSubject.SetMeasureMent("300", "200", "100");
            
            pSubject.removeRegister(pObserver3);

            pSubject.SetMeasureMent("1000", "1000", "1000");
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值