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 );}}