This article mainly introduces the Observer observer pattern of C # design pattern to solve the problem of Newton Children's shoes, simply narrates the principle of the observer pattern and analyzes the specific steps of using the observer pattern to solve the problem of Newton Children's shoe performance, and also comes with the demo source code for the reader to download the reference. , the need for friends can refer to the next
This paper describes the Observer Observer pattern of C # design pattern to solve the problem of Newton children's shoes. Share to everyone for your reference, as follows:
I. Theoretical definition
The Observer pattern describes a one-to-many relationship. When the state of an object changes, other objects are notified of changes. and respond accordingly.
Two. Application examples
Requirements Description: Newton's final exam results (score) came out, all the teachers want to know their students performance situation!
The Language Teacher (Teacherchinese) only cares about Newton's language (Chinese) achievement.
The English teacher (teacherenglish) only cares about Newton's English results.
The math teacher (teachermathematics) only cares about Newton's Math (mathematics) grades.
The class teacher wants to care about (Teacherteacherhead) Newton's various subjects and the total score (totalscore).
After the results, all the teachers have been informed (Notify).
Three. Specific code
1. Add 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>//// < ;/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 Notifyeventhandler (score score), public class score {public score () {}//Event declaration public Notifyeve Nthandler Notifyevent=null; <summary>///Call Entry///</summary> public void Notify () {onnotifychange (); }///<summary>//Notify Event///</summary> private void Onnotifychange () {if (notifyevent! = null) {not Ifyevent (this); }}///<summary>///Math score///</summary> public float Mathematics {get; set;} <summary>///English grade////</summary> public float 中文版 {get; set;} <summary>///Chinese results///</summary> public float Chinese {get; set;} <summary>///three subjects///</summary> public float Totalscore {get {return Mathematics+english+chin Ese }}///<summary>//Students basic information///</summary> public Student Student {get;Set } }}
3. Language Teacher
Using system;using system.collections.generic;using system.linq;using system.text;namespace Com.Design.Gof.Observer {//<summary>///Language Teacher///</summary> public class Teacherchinese {public void Receive (Score score) {
console.writeline ("I am a Chinese teacher, I only care about" +score.student.name+ "of the language 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 + "min"); } }}
5. Math teacher
Using system;using system.collections.generic;using system.linq;using system.text;namespace Com.Design.Gof.Observer {//<summary>///math teacher///</summary> public class Teachermathematics {public void Receive (Score score) { Console.WriteLine ("I am a math teacher, I only care about" + Score.student.Name + "Math score:" + score. Mathematics + "Min"); } }}
6. Head teacher
Using system;using system.collections.generic;using system.linq;using system.text;namespace Com.Design.Gof.Observer { //<summary>///Supervisor///</summary> public class Teacherteacherhead {public void Receive (score score) { string name=score.student.name; Console.WriteLine ("I am the head teacher, I want to care about" + name + "The various subjects and overall score"); Console.WriteLine (name + "The language score:" + score. Chinese + "min"); Console.WriteLine (name + "English score:" + score. 中文版 + "min"); Console.WriteLine (name + "Math score:" + score. Mathematics + "Min"); Console.WriteLine (name + "total:" + score. Totalscore + "min"); } }}
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 */Sco Re score = new Score {Chinese = $, mathematics = +, 中文版 = N, student = new Student {Name = "Newton"} }; Teacherchinese Teacherchinese = new Teacherchinese (); Chinese teacher teacherenglish teacherenglish = new Teacherenglish ();//english teacher teachermathematics teachermathematics = new Teacher Mathematics ();//math teacher Teacherteacherhead teacherteacherhead = new Teacherteacherhead ();//class teacher//Newton's report card came out, the teacher would like to know the result. Score. Notifyevent + = new Notifyeventhandler (teacherchinese.receive); Score. Notifyevent + = new Notifyeventhandler (teacherenglish.receive); Score. Notifyevent + = new Notifyeventhandler (teachermathematics.receive); Score. Notifyevent + = new Notifyeventhandler (teacherteacherhead.receive); To each subject to send targeted teachers, interested in the results of score. Notify (); #eNdregion Console.readkey (); } }}
8. Running Results
9. Summary
The use of events and notifications from the C # language allows for a more elegant implementation of the observer pattern. The + = operation of the event is really so happy.