Home > Article > Backend Development > An example of how the Observer pattern in C# solves the problem of Newton's children's shoes
This article mainly introduces the Observer pattern of C# design pattern to solve the problem of Newton's children's shoes. It briefly describes the principle of the observer pattern and analyzes the specific steps of using the observer pattern to solve the problem of Newton's children's shoes combined with specific examples. Relevant operating skills are provided, and the demo source code is attached for readers to download and refer to. Friends in need can refer to
. This article describes the Observer pattern of C# design pattern to solve the problem of Newton's children's shoes. Share it with everyone for your reference, as follows:
1. Theoretical definition
The observer pattern describes a one-to-many relationship. When the state of an object changes, other objects will be notified of the change. and react accordingly.
2. Application examples
Description of requirements: Newton’s students’ final exam scores (Score) have come out, and teachers in all subjects want to know their students’ scores!
Chinese teacher (TeacherChinese) only cares about Newton’s Chinese (Chinese) scores.
English teacher (TeacherEnglish) only cares about Newton’s English (English) scores.
Mathematics teacher (TeacherMathematics) only cares about Newton’s mathematics ( Mathematics) results.
The class teacher wants to care about (TeacherTeacherHead) Newton’s scores in each subject and the total score (TotalScore).
After the results come out, the teachers in each subject will be notified (Notify).
3. Specific coding
1. Add the student information class, which has only one Name attribute.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Observer { /// <summary> /// 学生信息类 /// </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() { } //事件声明 public NotifyEventHandler NotifyEvent=null; /// <summary> /// 调用入口 /// </summary> public void Notify() { OnNotifyChange(); } /// <summary> ///通知事件 /// </summary> private void OnNotifyChange() { if (NotifyEvent != null) { NotifyEvent(this); } } /// <summary> /// 数学成绩 /// </summary> public float Mathematics { get; set; } /// <summary> /// 英语成绩 /// </summary> public float English { get; set; } /// <summary> /// 语文成绩 /// </summary> public float Chinese { get; set; } /// <summary> /// 三科总成绩 /// </summary> public float TotalScore { get { return Mathematics+English+Chinese; } } /// <summary> /// 学生基本信息 /// </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("我是语文老师,我只关心"+score.student.Name+"的语文成绩:"+score.Chinese+"分"); } } }
4.English teacher
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Observer { /// <summary> /// 英语老师 /// </summary> public class TeacherEnglish { public void Receive(Score score) { Console.WriteLine("我是英语老师,我只关心" + score.student.Name + "的英语成绩:" + score.English + "分"); } } }
5.Math teacher
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Observer { /// <summary> /// 数学老师 /// </summary> public class TeacherMathematics { public void Receive(Score score) { Console.WriteLine("我是数学老师,我只关心" + score.student.Name + "的数学成绩:" + score.Mathematics + "分"); } } }
6 .Class teacher
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Observer { /// <summary> /// 班主任 /// </summary> public class TeacherTeacherHead { public void Receive(Score score) { string name=score.student.Name; Console.WriteLine("我是班主任,我要关心 " + name + " 的各科成绩和总成绩"); Console.WriteLine(name + "的 语文 成绩: " + score.Chinese + " 分"); Console.WriteLine(name + "的 英语 成绩: " + score.English + " 分"); Console.WriteLine(name + "的 数学 成绩: " + score.Mathematics + " 分"); Console.WriteLine(name + "的 总 成绩: " + score.TotalScore + " 分"); } } }
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 /*牛顿同学的成绩单*/ Score score = new Score { Chinese = 60, Mathematics = 100, English = 90, student = new Student { Name = "牛顿" } }; TeacherChinese teacherChinese = new TeacherChinese(); //语文老师 TeacherEnglish teacherEnglish = new TeacherEnglish();//英语老师 TeacherMathematics teacherMathematics = new TeacherMathematics();//数学老师 TeacherTeacherHead teacherTeacherHead = new TeacherTeacherHead();//班主任 //牛顿成绩单出来了,老师都想知道这个结果。 score.NotifyEvent += new NotifyEventHandler(teacherChinese.Receive); score.NotifyEvent += new NotifyEventHandler(teacherEnglish.Receive); score.NotifyEvent += new NotifyEventHandler(teacherMathematics.Receive); score.NotifyEvent += new NotifyEventHandler(teacherTeacherHead.Receive); //向 各 学科 老师发送 有针对性的,感兴趣的 成绩 score.Notify(); #endregion Console.ReadKey(); } } }
8.Run result
9. Summary
Using the events and notifications provided by the C# language can make the observer pattern more elegantly implemented. The += operation of events is really so happy.
The above is the detailed content of An example of how the Observer pattern in C# solves the problem of Newton's children's shoes. For more information, please follow other related articles on the PHP Chinese website!