Design Pattern learning path-Observer Pattern

Source: Internet
Author: User
Document directory
  • Observer Effect
  • Motivation:
  • Intent:
  • Structure:
  • Code implementation:
  • Observer mode (Event Events) in. NET Framework)
  • Key points of the observer Mode
Observer Effect

The observer promotes abstract coupling of the target, and the target does not know the details of any observer. But this also has potential disadvantages: when the data in the target changes progressively, the observer must be updated continuously or repeatedly. If updates are costly, it is necessary to introduce a policy for managing changes so that the observer is not notified multiple or frequently.

When a customer (observer) modifies the underlying data, you need to decide which object triggers the update notification sent to other observers. If the target notifies all observers after it is changed, each customer does not need to remember to trigger the notification. On the other hand, multiple consecutive updates are triggered. If the customer tells the target when to notify other customers, this continuous notification will be avoided. However, the customer adds the responsibility to tell the target when to send notifications. If a customer forgets it, the program cannot run normally.

Finally, based on the type or scope of the change, the observer defines multiple update Methods for receiving notifications to specify the notification type to be sent. In some cases, the customer can ignore some update notifications.

Motivation:

In the process of building software, we need to establish a "Notification dependency" for some objects-the state of an object (target object) changes, and all dependent objects (Observer objects) will be notified. If such dependency is too tight, the software cannot well resist changes.

Using object-oriented technology, you can weaken this dependency and form a stable dependency. To achieve loose coupling of the software architecture.

Intent:

Defines a one-to-many dependency between objects so that when the status of an object changes, all objects dependent on it are notified and automatically updated.

-- Design Pattern gof

Structure:

 

Code implementation:
Using system; using system. collections. generic; using system. text; namespace observer {class program {static void main (string [] ARGs) {subject = new bankaccount (); iaccountobserver email = new email (); iaccountobserver Tel = new telphone (); Subject. addobserver (email); Subject. addobserver (TEL); Subject. withikcount (100); console. readkey () ;}} public class useraccountargs {private string address; Public String address {get {return address;} set {address = value ;}} private string mobile; public String mobile {get {return mobile;} set {mobile = value ;}} public class email: iaccountobserver {public void Update (useraccountargs ARGs) {console. writeline (ARGs. address) ;}} public class telphone: iaccountobserver {public void Update (useraccountargs ARGs) {console. writeline (ARGs. mobile) ;}} public interface iaccountobserver {void Update (useraccountargs ARGs);} public class bankaccount: Subject {public override void withaccount (INT data) {useraccountargs ARGs = new useraccountargs (); args. address = "email notification account amount:" + data. tostring (); args. mobile = "SMS notification account amount:" + data. tostring (); y (ARGs) ;}} public abstract class subject {list <iaccountobserver> observerlist = new list <iaccountobserver> (); protected virtual void Policy (useraccountargs ARGs) {foreach (iaccountobserver observer in observerlist) {observer. update (ARGs) ;}} public void addobserver (iaccountobserver observer) {observerlist. add (observer);} public void removeobserver (iaccountobserver observer) {observerlist. remove (observer);} public abstract void withaccount (INT data );}}

 

Observer mode (Event Events) in. NET Framework)

 

Using system; using system. collections. generic; using system. text; namespace observerevent {class program {static void main (string [] ARGs) {bankaccount = new bankaccount (); emailer = new emailer (); mobile explorer = new mobile (); bankaccount. accountchange + = new accountchangeeventhandler (emailer. update); bankaccount. accountchange + = new accountchangeeventhandler (explorer. update); B Ankaccount. withikcount (100); console. readkey () ;}} public class useraccounteventargs: eventargs {private string address; Public String address {get {return address;} set {address = value ;}} private string mobile; public String mobile {get {return mobile;} set {mobile = value ;}} Public Delegate void accountchangeeventhandler (Object sender, useraccounteventargs E); public class bank Account {public event accountchangeeventhandler accountchange; protected virtual void onaccountchange (useraccounteventargs e) {If (this. accountchange! = NULL) {This. accountchange (this, e) ;}} public void withaccount (INT data) {useraccounteventargs ARGs = new useraccounteventargs (); args. address = "email notification account amount:" + data. tostring (); args. mobile = "SMS notification account amount:" + data. tostring (); onaccountchange (ARGs) ;}} public class emailer {public void Update (Object sender, useraccounteventargs e) {console. writeline (E. address) ;}} public class mobile {public void Update (Object sender, useraccounteventargs e) {console. writeline (E. mobile );}}}

 

Key points of the observer Mode

1. Using object-oriented abstraction, the observer mode allows us to change the object and observer independently, so that the dependency between the two can be loosely coupled.

2. When the target sends a notification, no observer is required. The notification (which can carry the notification information as a parameter) will be automatically transmitted. The observer decides whether to subscribe to notifications by himself, and the target object knows nothing about this.

3. In the C # event, the delegate acts as an abstract observer interface, and the object that provides the event acts as the target object. Delegation is a more loosely coupled design than the abstract observer interface.

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.