Design Pattern learning: Observer pattern (Observer, behavior pattern) (7)

Source: Internet
Author: User

1. The observer mode is also called the publish-subscribe mode.

2. 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.

3. Benefits of using the observer mode: maintain consistency between related objects. We do not want to make all kinds of Close coupling to maintain consistency, which will cause inconvenience to maintenance, expansion and reuse.
When to use the observer mode: when an object needs to 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.

Private Static void main (string [] ARGs) {boss = new boss (); boss. update + = new stockobserver ("stock brother", boss ). closestockmarket; boss. update + = new nbaobserver ("NBA brother", boss ). closenbadirectseeding; boss. subjectstate = "the boss is back... "; Boss. notify () ;}/// <summary> /// notification recipient interface /// </Summary> Public interface isubject {void notify (); string subjectstate {Get; set ;}} Public Delegate void eventhandler (); public class BOSS: isubject {public event eventhandler update; Public void every Y () {If (update! = NULL) {Update () ;}} Public String subjectstate {Get; Set ;}} /// <summary> /// foreground Encryption Class /// </Summary> public class secretary: isubject {public event eventhandler update; Public void every Y () {If (update! = NULL) {Update () ;}} Public String subjectstate {Get; Set ;}} /// <summary> /// colleague who views the stock /// </Summary> public class stockobserver {private string name; private isubject subject; Public stockobserver (string name, isubject subject) {This. name = Name; this. subject = subject;} public void closestockmarket () {console. writeline ("{0} {1} Close the stock market and continue to work! ", Subject. subjectstate, name) ;}/// <summary> /// colleagues watching the NBA /// </Summary> public class nbaobserver {private string name; private isubject subject; public nbaobserver (string name, isubject subject) {This. name = Name; this. subject = subject;} public void closenbadirectseeding () {console. writeline ("{0} {1} disable nbalivestream and continue to work! ", Subject. subjectstate, name );}}

 

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.