Observer mode _ update the client list

Source: Internet
Author: User

Observer mode highlights:

1. The observer mode defines the one-to-many relationship between objects.

2. The topic (that is, the observer) uses a common interface to update the observer.

3. The observer and the observer are loosely coupled. The observer does not know the details of the observer, but only knows that the observer implements the observer interface.

4. When using this mode, you can push or pull data from the observer

5. When multiple observers exist, the specified notification order cannot be customized.

Observer mode: defines one-to-many dependencies between objects. In this way, when an object changes its state, all objects dependent on it will receive notifications and update automatically.

Structure Summary: The synthesis relationship between a topic role and an abstract observer role indicates that a specific topic object can be referenced by any number of abstract observer objects. The use of abstract Observer instead of specific Observer means that the topic object does not need to know which specific Observer types are referenced, but only the abstract Observer type. This allows a specific topic object to dynamically maintain a series of references to the observer object and call the Update () method shared by each observer as needed. This is called "abstract programming ".

 

Advantages and disadvantages of observer mode:

The advantage of the Observer mode is that the presentation layer and the data logic layer are separated, and a stable update message transmission mechanism is defined. The categories are clear and the update interface is abstracted, this makes it possible to have a variety of presentation layers (observer ). However, the disadvantage is that each appearance object must inherit the interface class derived from this image, which makes it inconvenient. For example, a appearance object written by someone else does not inherit this abstract class, or the interface is incorrect, and we want to directly use it without modifying the class. Although the Adapter mode can be applied to solve this problem to a certain extent, it will cause more complicated and cumbersome design and increase the chance of errors.

The observer mode has the following advantages:

(1) The observer mode establishes an abstract coupling between the observer and the observer. What the observer role knows is that a specific observer aggregates, and each specific observer conforms to an abstract observer interface. The observer does not know any specific observer, but only knows that they all have a common interface. Since the observer and observer are not closely coupled, they can belong to different abstract layers.

(2) The observer mode supports broadcast communication. The observer sends a notification to all registered observers.

The observer mode has the following Disadvantages:

(1) It takes a lot of time to notify all observers if they have many direct and indirect observers.

(2) If there is a circular dependency between the observer, the observer will trigger a circular call between them, resulting in system crash. Pay special attention to this when using the observation test mode.

(3) If the notification to the observer is asynchronously delivered through another thread, the system must ensure that the delivery is carried out in an appropriate manner.

(4) Although the observer mode can enable the observer to know the changes of the observed object at any time, the observer mode does not have a mechanism to let the observer know how the observed object changes.

 

Example 1,

Use the observer mode to design the user list maintenance function between the server and the client.

Steps:

1. The server starts listening

2. Client Requests connection

3. The server saves the list of connected clients and updates the list of all clients.

4. Close the connection on the client

5. The server deletes closed clients from the list and updates the list of all clients.

Observer: client.

Observer: List of server-side users.

 

Class details:

 

Code Section:

ISubject. cs

 Public interface ISubject
{
// Add Registration
ISubject register (IObserver Observer );

// Cancel registration
Void unregister (IObserver Observer );

// Notify the observer
Void notifyObserver ();
}

IObserver. cs

 Public interface IObserver
{
// Obtain the name of each observer
String getName ();

// Used to obtain the information changed by the observer
Void Policy (List <IObserver> ObserverList );
}

 

IShow. cs

 Public interface IShow
{
// Display the updated user list
Void show (List <IObserver> Observer );
}

Subject. cs

 Public class Subject: ISubject
{
// Save the observer user list
List <IObserver> ObserverList = new List <IObserver> ();

Public ISubject register (IObserver Observer)
{
Lock (ObserverList)
{
ObserverList. Add (Observer );
NotifyObserver ();
Return this;
}
}

Public void unregister (IObserver Observer)
{
Lock (ObserverList)
{
If (ObserverList. Contains (Observer ))
{
ObserverList. Remove (Observer );
NotifyObserver ();
}
}
}

Public void policyobserver ()
{
Lock (ObserverList)
{
If (ObserverList. Count> 0)
{
Foreach (IObserver item in ObserverList)
{
Item. Y (ObserverList );
}
}
Else
{
Console. WriteLine ("-------------------------------");
Console. WriteLine ("no user is online currently ");
Console. Read ();
}
}
}

}

Observer1.cs

 Public class Observer1: IObserver, IShow
{
Public string name = "XX ";

// Used to save the object to be observed and cancel registration
Public ISubject obj;

// Register with the observer list
Public void register (ISubject sub)
{
Obj = sub. register (this );
}

// Cancel registration from the list of observers
Public void unregister ()
{
If (obj! = Null)
{
Obj. unregister (this );
}
}

// Observer interface, used to obtain the information changed by the observer
Public void Policy (List <IObserver> ObserverList)
{
Show (ObserverList );
}

// Obtain the name of each observer
Public string getName ()
{
Return name;
}

// Display the updated observer list
Public void show (List <IObserver> ObserverList)
{
Console. WriteLine ("----------- user list --------");
Foreach (IObserver item in ObserverList)
{
Console. WriteLine (item. getName ());
}
}
}

Program. cs

 Class Program
{
Static void Main (string [] args)
{
// The instance is observed.
Subject sub = new Subject ();

// Instance observer
Observer1 ob1 = new Observer1 ();
// Register with the observer
Ob1.register (sub );

Observer2 ob2 = new Observer2 ();
Ob2.register (sub );

Console. Read ();
Console. WriteLine ("cancel registration ..... ");

// Cancel registration
Ob1.unregister ();
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.