Observer mode "design mode"

Source: Internet
Author: User

Observer Pattern is a software design pattern in which an object, called the subject, maintains a list of its DEP Endents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems. The Observer pattern is also a key part in the familiar Model–view–controller (MVC) architectural pattern.

[1]

The Observer pattern is implemented in numerous programming libraries and systems, including almost all GUI toolkits.
The Observer pattern is a software design pattern. In the observer pattern, there is a target that manages to maintain a pile of observers that are dependent on its state, informing all observers when the state of the target changes, by invoking the method exposed by the observer. The observer pattern, which is typically used to implement the event distribution processing system.
The Observer pattern is an important part of the MVC architecture. The observer pattern is applied in the implementation of almost all GUI components.
Viewer mode:
1. The viewer
2. Target
The Observer adds a member variable that is registered to the target as the target.
When the state of the target changes, call the Observer's provided notification method, telling the observer that the current target state has changed.
This is the Observer pattern.
Look at the diagram to understand the meaning of the Observer pattern:

Issues that can occur with the viewer:
The Observer pattern can cause memory leaks, known as the lapsed listener problem, because in basic implementation it requ Ires both explicit registration and explicit deregistration, as in the Dispose pattern, because the subject holds strong R Eferences to the observers, keeping them alive. This can is prevented by the subject holding weak references to the observers.
 a memory leak occurs. Because the target holds a strong reference to the observer, when the observer is to be destroyed, the Observer object is not recycled because other objects hold the observer's reference. However, this can be done by allowing the target to simply hold the observer's weak reference to avoid the problem. 
C #:
Using system;using System.Collections; namespace wikipedia.patterns.strategy{//IObserver-Interface for the Observer public interface Iobse RVer {//called by the subject-update the observer of any change//the method PA        Rameters can modified to fit certain criteria void Update (string message);                } public class Subject {//Use array list implementation for collection of observers                 Private ArrayList observers;                Constructor public Subject () {observers = new ArrayList (); } public void Register (IObserver observer) {//If List does ES not contain observer, add if (!observers. Contains (Observer)) {observers.                        ADD (Observer);      }          } public void Deregister (IObserver observer) {//if Obser Ver is in the list, remove if (observers. Contains (Observer)) {observers.                        REMOVE (Observer); }} public void Notify (String message) {//Call UPDA                                Te method for every observer foreach (IObserver Observer in observers) { Observer.                        Update (message);         }}}//Observer1-Implements The IObserver public class Observer1:iobserver {public void Update (String message) {Console.WriteLine ("Obs                Erver1: "+ message); }}//Observer2-Implements The IObserver public class Observer2:iobserver {public void Update (String message) {Con Sole.                WriteLine ("Observer2:" + message); }}//Test class public class Observertester {[STAThread] Pub                        Lic static void Main () {Subject mysubject = new Subject ();                        IObserver myObserver1 = new Observer1 ();                         IObserver myObserver2 = new Observer2 ();                        Register observers Mysubject.register (MYOBSERVER1);                         Mysubject.register (MYOBSERVER2);                        Mysubject.notify ("message 1");                Mysubject.notify ("Message 2"); }        }}

Observer mode design mode

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.