1. The Observer Pattern concept
The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time, and the subject object notifies all observer objects when the state changes, enabling them to automatically update their behavior.
The Observer pattern realizes the separation of the presentation layer and the data logic layer, defines the stable update message passing mechanism, and abstracts the update interface so that there can be a variety of different presentation layers, namely the observer.
The Observer pattern creates an abstract coupling between the observer and the Observer, and the observer does not know of any specific observer, but simply holds the list of abstract observers, each of which conforms to the interface of an abstract observer.
2. Business Scenario
The simulation management system pushes the latest message to the corresponding user, and the observer pattern is a good way to simulate such a scenario, enabling the decoupling between the Publisher and the Subscriber.
3. Actual combat
3.1 Defining Entity objects
Defining a Subscriber entity class
namespacesubscriberdesignwithredis.model{ Public classUser {/// <summary> ///User ID/// </summary> Public stringuserId {Get;Set; } /// <summary> ///User name/// </summary> Public stringUserName {Get;Set; } }}
Here list<user> is used to store subscribers ' lists
namespacesubscriberdesignwithredis.model{[Serializable] Public classInformation {/// <summary> ///Publisher Collection/// </summary> PublicList<user> userlist{Get;Set; } /// <summary> ///Signal/// </summary> Public stringSignal {Get;Set; } /// <summary> ///message/// </summary> Public stringinfo {Get;Set; } }}
3.2 Publishers
/// <summary> ///Portal Publishing Information/// </summary> Public classPortalpublish {/// <summary> ///defines a delegate method that acts as a subscriber interface/// </summary> /// <param name= "obj" ></param> Public Delegate voidNotifysubscribeeventhandler (Objectobj); /// <summary> ///Publish message Inner class/// </summary> Public classPublishinfo { PublicNotifysubscribeeventhandler notifyevent; /// <summary> ///Message Entity/// </summary> PublicInformation Information {Get;Set; } /// <summary> ///Overloaded Constructors/// </summary> /// <param name= "Information" >Message Entity</param> PublicPublishinfo (Information information) { This. Information =information; } /// <summary> ///New/// </summary> /// <param name= "ob" ></param> Public voidaddsubscribe (Notifysubscribeeventhandler ob) {notifyevent+=ob; } /// <summary> ///Delete/// </summary> /// <param name= "ob" ></param> Public voidremovesubscribe (Notifysubscribeeventhandler ob) {notifyevent-=ob; } /// <summary> ///update (using LAMDA expressions)/// </summary> Public voidUpdate () = Notifyevent?. Invoke ( This); } Public classPortalpublishinfo:publishinfo { PublicPortalpublishinfo (Information information):Base(information) {} }}
3.3 Subscribers
namespacesubscriberdesignwithredis{ Public classSubscriber { Public stringName {Get;Set; } PublicSubscriber (stringname) { This. Name =name; } /// <summary> ///Receive/// </summary> /// <param name= "obj" ></param> Public voidRecevie (Objectobj) {Portalpublish.portalpublishinfo Portalpublishinfo= obj asPortalpublish.portalpublishinfo; if(Portalpublishinfo! =NULL) {Console.WriteLine ("notified {0} of {1} ' s"+"Info is: {2}", Name,
PortalPublishInfo.Information.signal, PortalPublishInfo.Information.info); } } }}
3.4 Test Class
namespacesubscriberdesignwithredis{classProgram {Static voidMain (string[] args) { //A collection of simulated subscribersList<user> userlist =NewList<user>() { NewUser () {userId ="1", UserName ="Ozil" }, NewUser () {userId ="2", UserName ="Alexis"} }; //Defining Publishing DataInformation information =Newinformation () {signal="Publish Info", Info="This was a new information for each client", UserList=userlist}; Portalpublishinfo Portalpublish=NewPortalpublishinfo (information); Console.WriteLine ("Add Subscribers"); //loop subscribers list, send messages to specific subscribers foreach(User Userinchuserlist) {Subscriber Client=NewSubscriber (User.username); Portalpublish.addsubscribe (NewNotifysubscribeeventhandler (client. Recevie)); } //Execution Eventsportalpublish.update (); Console.WriteLine ("--------------------------------------------"); Console.WriteLine ("Remove Subscribers"); foreach(User Userinchuserlist) {Subscriber Client=NewSubscriber (User.username); Portalpublish.removesubscribe (NewNotifysubscribeeventhandler (client. Recevie)); } //Execution Eventsportalpublish.update (); Console.ReadLine (); } }}
4. Operation result
C # Design pattern (3)--simulate push messages with viewer mode