On observer model
Observer mode
definition
It refers to the one to many dependency between multiple objects. When the state of an object changes, all objects that depend on it are notified and automatically updated. This mode is sometimes called publish subscribe mode, which is an object behavior mode.
Take a common example
There is an example:
Some customers are very interested in the products of a store (such as apple thirteen incense). Customers can check whether they have arrived every day. In most cases, they will return empty handed.
Of course, the store can inform customers that they have arrived, but they are likely to be considered as deliberately harassed by customers.
We can ask interested customers (observers) to tell the store (publisher) that when the thirteen spices arrive, the store (publisher) will notify customers (observers).
Key object
Publisher: to notify other objects of their own state changes
Observer: an object that wants to follow (subscribe to) changes in the publisher's state
abstract
When customer A (observer A) tells the store by email and customer B (observer b) tells the store by XX, the store (publisher) will have A serious waste of resources (the publisher has to deal with different subscription methods).
So we need a specification (observer interface)
Main object
Observer interface: it is an abstract class or interface, which contains an abstract method to update itself. It is called when receiving the change notification from Publisher.
Concrete Observer: implement the method defined by the interface in the abstract observer to update its own state when notified of the change of the target.
Publisher: the observed target. It implements the notification method. When the internal state changes, it notifies all registered observer objects.
C # simple code example
Click to view the code//subscriber public class Publisher { public List<IObserver> Observers { get; set; } = new List<IObserver>(); //Subscription (this method name is for easy understanding, and the general method name is Subscribe) //In fact, in the observer mode, both Publisher and observer can subscribe. public void AddObserver(IObserver observer) { Console.WriteLine($"Publisher: I'm Add {observer.GetType().Name}"); this.Observers.Add(observer); } //UnSubscribe (this method name is for easy understanding, and the general method name is UnSubscribe) public void DeleteObserver(IObserver observer) { Console.WriteLine($"Publisher: I'm Delete {observer.GetType().Name}"); this.Observers.Remove(observer); } //notice private void Notify() { Console.WriteLine("Publisher: I arrived"); foreach (var item in Observers) { item.onGetNotify(this); } } } //Observer interface public interface IObserver { //Response when receiving the notification (this method name is for easy understanding, and the general method name is next) public void onGetNotify(Publisher publisher); } public class ObserverA : IObserver { public void onGetNotify(Publisher publisher) { System.Console.WriteLine($"{this.GetType().Name} I got it!"); } } public class ObserverB : IObserver { public void onGetNotify(Publisher publisher) { System.Console.WriteLine($"{this.GetType().Name} I got it!"); } }
Calling code
Click to view the codevar publisher = new Publisher(); var observerA = new ObserverA(); var observerB = new ObserverB(); publisher.AddObserver(observerA); publisher.AddObserver(observerB); publisher.Notify(); publisher.DeleteObserver(observerA); publisher.Notify(); publisher.DeleteObserver(observerB); publisher.Notify();
Operation results:
Observe in. net
. net provides two interfaces to facilitate developers to create observer mode
IObserver < T > provides a mechanism for receiving push based notifications. (publisher)
Iobserveable < T > defines the provider of push based notifications. (observer)
MSDN documents are very detailed, so I won't repeat them here. Interested can have a look[ https://docs.microsoft.com/zh-cn/dotnet/api/system.iobserver-1?view=net-6.0]