Design Pattern 18:observer Observer pattern (behavioral mode)

Source: Internet
Author: User

Observer Observer pattern (behavioral mode)

Motive (motivation)

In the process of software building, we need to establish a "notification dependency" for some objects--the state of an object (the target object) is changed, and all dependent objects (The Observer object) will be notified. If the object relationship is too close, the software will not be able to resist the change well.

Using object-oriented technology, you can weaken this dependency and form a stable dependency. So as to realize the loose coupling of software architecture.

Intentions (Intent)

Defines a one-to-many dependency between objects so that when the state of an object changes, all objects that depend on it are notified and updated automatically-design mode GoF

Sample code

This is an example of an ATM taking money:

     Public classATM {PrivateBankAccount BankAccount;  //...        voidProcess (intdata)        {Bankaccount.widthdraw (data); }    }     Public classBankAccount {PrivateEmailer Emailer;//Strong dependency Relationships        PrivateMobile mobile;//Strong dependency Relationships         Public voidWidthdraw (intdata) {//...            Useraccountargs args=new Useraccountargs (); //...        Emailer. SendEmail (Useraccountargs.            UserEmail); Mobile. SendNotification (Useraccountargs.        Mobilenumber); }    }     Public classEmailer { Public voidSendEmail (stringuseremail) {            //...        }    }     Public classMobile { Public voidSendNotification (stringMobilenumber) {            //...}    }
public class Useraccountargs { public string UserEmail { get; set;} public string Mobilenumber { get; set;}}

BankAccount and Emailer, mobile are tightly coupled relationships that need decoupling:

 Public classBankAccount {IList<IAccountObserver> observerlist=NewList<iaccountobserver>();  Public voidWidthdraw (intdata) {            //...Useraccountargs args=NewUseraccountargs (); //...            foreach(varAccountobserverinchobserverlist)            {accountobserver.update (args); }        }         Public voidaddobserver (Iaccountobserver accountobserver) {observerlist.add (accountobserver); }         Public voidremoveobserver (Iaccountobserver accountobserver) {observerlist.remove (accountobserver); }    }     Public InterfaceIaccountobserver {voidUpdate (Useraccountargs args); }     Public classEmailer:iaccountobserver {//Public void SendEmail (string to)//{        //    //...        //}         Public voidUpdate (Useraccountargs args) {stringUserEmail =args.            UserEmail; //...        }    }     Public classMobile:iaccountobserver {//Public void SendNotification (string to)//{        //    //...        //}         Public voidUpdate (Useraccountargs args) {stringMobilenumber =args.            Mobilenumber; //...        }    }     Public classUseraccountargs { Public stringUserEmail {Get;Set; }  Public stringMobilenumber {Get;Set; } }

If the bankaccount changes more, you can continue to abstract to de-iaccountobserver the coupling:

     Public classBankaccount:subject { Public voidWidthdraw (intdata) {            //...Useraccountargs args=NewUseraccountargs (); //...Notify (args); }    }     Public Abstract classSubject {IList<IAccountObserver> observerlist =NewList<iaccountobserver>();  Public voidNotify (Useraccountargs args) {//...            foreach(varAccountobserverinchobserverlist)            {accountobserver.update (args); }        }         Public voidaddobserver (Iaccountobserver accountobserver) {observerlist.add (accountobserver); }         Public voidremoveobserver (Iaccountobserver accountobserver) {observerlist.remove (accountobserver); }    }

At this time BankAccount and iaccountobserver decoupling.

Evolutionary process

When writing software, it is not necessary to apply a design pattern. In order to cope with the change, in the process of decoupling, there is a natural use of a pattern.

What is important is loosely coupled design thinking. The meaning of learning design pattern is to deepen the design thinking.

Structure (Structure)

Several points of observer model

    • Using object-oriented abstraction, the Observer pattern allows us to independently change the target and the observer, thus Da Josong coupling the dependencies between them.
    • When a target sends a notification, there is no need to specify the observer, and the notification (which can carry notification information as an argument) is automatically propagated. The target object is unaware of the observer's own decision to subscribe to the notification.
    • In C # 's event, the delegate acts as the Observer interface, and the object that provides the event acts as the target object. A delegate is a design that is more loosely coupled than an abstract observer interface.

Reprint please specify the source:

Jesselzj
Source: http://jesselzj.cnblogs.com

Design Pattern 18:observer Observer pattern (behavioral 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.