First, preface
The observer pattern is also called the Publish-subscribe pattern.
The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time, and notifies all observer objects when the state of the subject is changed, so that they can automatically update themselves.
When do I use observer mode?
When a change to an object needs to change other objects at the same time.
Second, the structure diagram
Third, instance code
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceObserver Pattern {classProgram {Static voidMain (string[] args) { //boss amBoss Huhansan =NewBoss (); //colleagues who look at sharesStockobserver Tongshi1 =NewStockobserver ("Li Zhe", Huhansan); //look at the NBA colleagues.Nbaobserver Tongshi2 =NewNbaobserver ("Li Ming", Huhansan); Huhansan. Attach (TONGSHI1); Huhansan. Attach (TONGSHI2); Huhansan. Detach (TONGSHI1); //boss BackHuhansan. Subjectstate ="I huhan back! "; //Send a notificationHuhansan. Notify (); Console.read (); } } //Notifier Interface InterfaceSubject {voidAttach (Observer Observer); voidDetach (Observer Observer); voidNotify (); stringSubjectstate {Get; Set; } } classSecretary:subject {//Colleagues List PrivateIlist<observer> observers =NewList<observer>(); Private stringAction; //Increase Public voidAttach (Observer Observer) {observers. ADD (Observer); } //reduced Public voidDetach (Observer Observer) {observers. REMOVE (Observer); } //Notice Public voidNotify () {foreach(Observer oinchobservers) O.update (); } //Foreground Status Public stringSubjectstate {Get{returnAction;} Set{action =value;} } } classBoss:subject {//Colleagues List PrivateIlist<observer> observers =NewList<observer>(); Private stringAction; //Increase Public voidAttach (Observer Observer) {observers. ADD (Observer); } //reduced Public voidDetach (Observer Observer) {observers. REMOVE (Observer); } //Notice Public voidNotify () {foreach(Observer oinchobservers) O.update (); } //Boss Status Public stringSubjectstate {Get{returnAction;} Set{action =value;} } } //Abstract Viewer Abstract classObserver {protected stringname; protectedSubject Sub; PublicObserver (stringname, Subject sub) { This. Name =name; This. Sub =Sub; } Public Abstract voidUpdate (); } //colleagues who look at shares classStockobserver:observer { PublicStockobserver (stringname, Subject sub):Base(name, sub) {} Public Override voidUpdate () {Console.WriteLine ("{0} {1} Close stock Quotes and continue working! ", Sub. Subjectstate, name); } } //look at the NBA colleagues. classNbaobserver:observer { PublicNbaobserver (stringname, Subject sub):Base(name, sub) {} Public Override voidUpdate () {Console.WriteLine ("{0} {1} Close the NBA Live, continue to work! ", Sub. Subjectstate, name); } }}
Iv. deficiencies
In the above code, the abstract notifier relies on the abstract observer, and the Observer's method is the same name as the update (), if the Observer has a different name of the method, the use of event delegates to implement abstract notifier to the abstract observer's dependence, but also do not need the observer is the general update () method.
Because abstract notifier does not want to rely on abstract observers, the "increase", "reduce" The Observer's method is also unnecessary.
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceObserver Pattern {classProgram {Static voidMain (string[] args) { //boss HuhanBoss Huhansan =NewBoss (); //colleagues who look at sharesStockobserver Tongshi1 =NewStockobserver ("Li Zhe", Huhansan); //look at the NBA colleagues.Nbaobserver Tongshi2 =NewNbaobserver ("Li Ming", Huhansan); Huhansan. Update+=NewEventHandler (tongshi1. Closestockmarket); Huhansan. Update+=NewEventHandler (Tongshi2. closenbadirectseeding); //boss BackHuhansan. Subjectstate ="I huhan back! "; //Send a notificationHuhansan. Notify (); Console.read (); } } //Notifier Interface InterfaceSubject {voidNotify (); stringSubjectstate {Get; Set; } } //delegate for event handlers Delegate voidEventHandler (); classSecretary:subject {//declares an event update, type is delegate EventHandler Public EventEventHandler Update; Private stringAction; Public voidNotify () {Update (); } Public stringSubjectstate {Get{returnAction;} Set{action =value;} } } classBoss:subject {//declares an event update, type is delegate EventHandler Public EventEventHandler Update; Private stringAction; Public voidNotify () {Update (); } Public stringSubjectstate {Get{returnAction;} Set{action =value;} } } //colleagues who look at shares classStockobserver {Private stringname; PrivateSubject Sub; PublicStockobserver (stringname, Subject sub) { This. Name =name; This. Sub =Sub; } //Close Stock Quotes Public voidClosestockmarket () {Console.WriteLine ("{0} {1} Close stock Quotes and continue working! ", Sub. Subjectstate, name); } } //look at the NBA colleagues. classNbaobserver {Private stringname; PrivateSubject Sub; PublicNbaobserver (stringname, Subject sub) { This. Name =name; This. Sub =Sub; } //Close NBA Live Public voidclosenbadirectseeding () {Console.WriteLine ("{0} {1} Close the NBA Live, continue to work! ", Sub. Subjectstate, name); } }}
C # Object-oriented design-observer mode (17)