观察者模式

The Observer design pattern defines a one-to-many dependency between objects so that when one object changes states, all its dependents are notified and updated automatically.    

观察者模式——发布/订阅模式(publish/subscribe):定义了一种一对多的依赖关系,让多个观察者对象同时监听某一主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己。

UML Class Diagram

 

Subject: This is going to be an interface that declares the operations for adding and removing observers as well as sending notifications to the registered observers when the subject state changes.We can add any number of observers.

ConcreateSubject: This is going to be a concrete class and this class should implement the Subject interface. This class maitains its own state and when the state is changed, it will send notifications to all observers by calling the observer's update method.

Observer: This is going to be an interface that defines an updating interface for objects that should be notified when the subject state changes.

 ConcreteObserver: This is going to be a class implements the Observer interface. This class provides one method to determine what changes have occurred with the subject. This Observer method is going to be called by the Subject to send the notification.

Structure Code in C#

    public interface Subject
    {
        void Attach(Observer observer);
        void Detach(Observer observer);
        void Notify();
    }

    public class ConcreteSubject : Subject
    {
        private string state; 
        private List<Observer> observers = new List<Observer>();

        public void Attach(Observer observer)
        {
            observers.Add(observer);
        }

        public void Detach(Observer observer)
        {
            observers.Remove(observer);
        }

        public void Notify()
        {
            foreach (var observer in observers)
            {
                observer.Update();
            }
        }

        public string State
        {
            get { return state; }
            set
            {
                if (state != value)
                {
                    state = value;
                    Notify();
                }
            }
        }
    }
Subject
    public interface Observer
    {
       void Update();
    }

    public class ConcreteObserver : Observer
    {
        private string name;
        private ConcreteSubject subject;

        public ConcreteObserver(ConcreteSubject subject, string name)
        {
            this.subject = subject;
            this.name = name;
        }

        public void Update()
        {
            Console.WriteLine($"Observer {name} new state is {subject.State}.");
        }
    }
Observer
var subject = new ConcreteSubject();
subject.Attach(new ConcreteObserver(subject, "X"));
subject.Attach(new ConcreteObserver(subject, "Y"));
subject.Attach(new ConcreteObserver(subject, "Z"));

subject.State = "abc";
subject.State = "def";
Client

 

特点:

   当一个对象的改变需要同时改变其他对象时。而且它不知道具体有多少对象有待改变时,应该考虑使用观察者模式。

   一个抽象模型有两个方面,其中一方面依赖于另一方面,这时用观察者模式可以将这两者封装在独立的对象中使它们各自独立地改变和复用。

   观察者模式所做的工作其实就是解除耦合,让耦合的双方都依赖于抽象,而不是依赖于具体。从而使各自的变化都不会影响另一边的变化。

Typical Use Case:

A mailing list where every time an event happens(i.e. new product, a gathering, etc) a message needs to the users who subscribed to the list.

The company needs to notify all its employees of any decision they make. Here, the Company is the Subject, and the Employees are the Observers. Any change in the policy of the company and the company need to notify all its employees or you can say, Observers. This is a simple real-world example of the Observer Design Pattern. 

 

事件委托:

  委托就是一种引用方法的类型。一旦为委托分配了方法,委托将与方法具有相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值。委托可以看作使对函数的抽象,是函数的“类”,委托的实例将代表一个具体的函数。

  一个委托可以搭载多个方法,所有方法被依次唤醒。

  委托搭载的所有方法必须具有相同的原型和形式,也就是拥有相同的参数列表和返回值类型。

posted @ 2019-05-22 21:54  云霄宇霁  阅读(89)  评论(0编辑  收藏  举报