1. theoretical definition
The observer mode describes a one-to-many relationship. When the status of an object changes, other objects will be notified of changes. And respond accordingly.
2. Application Example
Requirement Description: Newton's final Score (Score) has come out. All the teachers in each subject want to know their student scores!
The Chinese teacher (TeacherChinese) only cares about Newton's Chinese scores.
TeacherEnglish only cares about Newton's English scores.
TeacherMathematics only cares about Newton's Mathematics scores.
The class teacher wants to care about Newton's scores and total scores (TotalScore ).
After the score is obtained, all the teachers in each subject are notified (Notify ).
3. Specific Encoding
1. Add a student information class with only one Name attribute.
Using System; using System. collections. generic; using System. linq; using System. text; namespace Com. design. gof. observer {// <summary> /// Student information // </summary> public class Student {// <summary> /// name /// </summary> public string Name {get; set ;}}}
2. Transcript (Score)
Using System; using System. collections. generic; using System. linq; using System. text; namespace Com. design. gof. observer {public delegate void policyeventhandler (Score); public class score {public Score () {}// event Declaration public policyeventhandler policyevent = null; /// <summary> /// call entry // </summary> public void every Y () {onpolicychange ();} /// <summary> /// notification event /// </summary> private void onid y Change () {if (policyevent! = Null) {policyevent (this) ;}/// <summary> // mathematical score /// </summary> public float Mathematics {get; set ;} /// <summary> // English score /// </summary> public float English {get; set ;} /// <summary> // Chinese score /// </summary> public float Chinese {get; set ;} /// <summary> /// total score of the three subjects /// </summary> public float TotalScore {get {return Mathematics + English + Chinese ;}} /// <summary> /// basic Student information /// </summary> public student Student {get; set ;}}}
3. Chinese teacher
Using System; using System. collections. generic; using System. linq; using System. text; namespace Com. design. gof. observer {// <summary> /// 文 /// </summary> public class TeacherChinese {public void Receive (Score score) {Console. writeLine ("I am a Chinese teacher, I only care about" + score. student. name + "Chinese score:" + score. chinese + "points ");}}}
4. English teacher
Using System; using System. collections. generic; using System. linq; using System. text; namespace Com. design. gof. observer {// <summary> // english teacher // </summary> public class TeacherEnglish {public void Receive (Score score) {Console. writeLine ("I am an English teacher, I only care about" + score. student. name + "English score:" + score. english + "points ");}}}
5. Math teacher
Using System; using System. collections. generic; using System. linq; using System. text; namespace Com. design. gof. observer {// <summary> /// math /// </summary> public class TeacherMathematics {public void Receive (Score score) {Console. writeLine ("I Am a math teacher, I only care about" + score. student. name + "Mathematical score:" + score. mathematics + "points ");}}}
6. class teacher
Using System; using System. collections. generic; using System. linq; using System. text; namespace Com. design. gof. observer {// <summary> // class teacher // </summary> public class TeacherTeacherHead {public void Receive (Score score) {string name = score. student. name; Console. writeLine ("I am the class teacher, I want to care about" + name + "scores and total scores of all subjects"); Console. writeLine (name + "Chinese score:" + score. chinese + "points"); Console. writeLine (name + "English score:" + score. english + "points"); Console. writeLine (name + "Mathematical score:" + score. mathematics + "points"); Console. total score of WriteLine (name + ":" + score. totalScore + "score ");}}}
7. The following is the main function call
Using System; using System. collections. generic; using System. linq; using System. text; using Com. design. gof. observer; namespace Com. design. gof. test {class Program {static void Main (string [] args) {# region Observer/* Newton's transcript */Score score = new Score {Chinese = 60, Mathematics = 100, english = 90, student = new Student {Name = "Newton" }}; TeacherChinese teacherChinese = new TeacherChinese (); // Chinese teacher Tea CherEnglish teacherEnglish = new TeacherEnglish (); // english teacher TeacherMathematics teacherMathematics = new TeacherMathematics (); // mathematics teacher TeacherTeacherHead teacherTeacherHead = new teacher (); // The class teacher // Newton's transcript is displayed. The teacher wants to know the result. Score. policyevent + = new policyeventhandler (teacherChinese. receive); score. policyevent + = new policyeventhandler (teacherEnglish. receive); score. policyevent + = new policyeventhandler (teacherMathematics. receive); score. policyevent + = new policyeventhandler (teacherTeacherHead. receive); // send targeted and interesting score to instructors of various disciplines. running y (); # endregion Console. readKey ();}}}
8. Running result
9. Summary
The event and notification provided by the C # language can make the observer mode more elegant. The + = operation of the event is So happy.
The attachment contains the program source code.
Download)