Observer mode analysis, structure diagram, and basic code

Source: Internet
Author: User

Observer mode analysis, structure diagram, and basic code
Zookeeper

Definition: The Observer mode defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves.
Structure:


Subject Class, which can be translated as a topic or abstract notification. It is generally implemented using an abstract class or an interface. It stores all references to the observer object in one aggregation, and each topic can have any number of observers. Abstract topic provides an interface to add and delete observers.
The Observe class is an abstract observer. It defines an interface for all the specific observers and updates itself when receiving notifications from the topic. This interface is called the update interface. Abstract observers are generally implemented using an abstract class or an interface. The Update interface usually contains an Update () method, which is called the Update method.
The ConcreteSubject class is called a specific topic or notification owner. It stores the status in a specific observer object. When the internal status of a specific topic changes, it sends a notification to all registered observers. A topic role is usually implemented by a specific subclass.
ConcreteObserver class, a specific observer, implements the update interface required by the abstract observer role, so that its status can be consistent with the theme. A specific observer role can save a reference pointing to a specific topic object. A specific observer role is usually implemented by a specific subclass.
Feature: the work done by the observer mode is actually decoupling. Let both sides of coupling depend on abstraction, rather than on specifics. So that their changes do not affect the changes on the other side.
Applicability: when an object does not change other objects at the same time, and it does not know how many objects need to be changed, the observer mode should be considered.
Deficiency: "abstract notifiers" still rely on "abstract notifiers". That is to say, in the absence of an interface like abstract observer, I cannot complete the notification function. In addition, each specific observer calls the 'update' method instead of the 'update' method. As mentioned earlier, what I hope is that the 'toolbox 'is hidden, the 'automatic Windows' is open, which is not a method of the same name at all. This should be a disadvantage.
Delegated knowledge:
A delegate is a type of reference method. Once a method is assigned to the Delegate, the delegate has the same behavior as the delegate. The use of delegate methods can have parameters and return values like any other method. The delegate can be seen as an abstraction of the function and a 'class' of the function. The delegated instance will take a specific function.
A single delegate can carry multiple methods, and all methods are awakened in turn. More importantly, it makes the methods carried by the delegate object do not need to belong to the same class. All methods carried by the delegate object must have the same prototype and form, that is, they have the same parameter list and return value type.
Basic code:

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace observer Mode
{
Class Program
{
Static void Main (string [] args)
{
ConcreteSubject s = new ConcreteSubject ();

S. Attach (new ConcreteObserver (s, "X "));
S. Attach (new ConcreteObserver (s, "Y "));
S. Attach (new ConcreteObserver (s, "Z "));

S. SubjectState = "ABC ";
S. Notify ();

Console. Read ();

}
}


Abstract class Subject
{
Private IList Observers = new List ();

// Add an observer
Public void Attach (Observer observer)
{
Observers. Add (observer );
}
// Remove the observer
Public void Detach (Observer observer)
{
Observers. Remove (observer );
}
// Notification
Public void Policy ()
{
Foreach (Observer o in observers)
{
O. Update ();
}
}
}

// Specific notification recipient
Class ConcreteSubject: Subject
{
Private string subjectState;

// Specific notification recipient status
Public string SubjectState
{
Get {return subjectState ;}
Set {subjectState = value ;}
}
}


Abstract class Observer
{
Public abstract void Update ();
}

Class ConcreteObserver: Observer
{
Private string name;
Private string observerState;
Private ConcreteSubject subject;

Public ConcreteObserver (
ConcreteSubject subject, string name)
{
This. subject = subject;
This. name = name;
}
// Update
Public override void Update ()
{
ObserverState = subject. SubjectState;
Console. WriteLine ("the new status of the observer {0} is {1 }",
Name, observerState );
}

Public ConcreteSubject Subject
{
Get {return subject ;}
Set {subject = value ;}
}
}

}

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.