本文实例讲述了C#设计模式之Observer观察者模式解决牛顿童鞋成绩问题。分享给大家供大家参考,具体如下:
一.理论定义
观察者模式 描述了 一种 一对多的关系。 当某一对象的状态发生改变时,其他对象会得到 改变的通知。并作出相应的反应。
二.应用举例
需求描述:牛顿同学的期末考试成绩(score)出来了,各科老师都想知道自己的 学生 成绩情况!
语文老师(TeacherChinese)只关心 牛顿的语文(Chinese)成绩.
英语老师(TeacherEnglish)只关心 牛顿的英语(English)成绩.
数学老师(TeacherMathematics)只关心 牛顿的数学(Mathematics)成绩.
班主任想关心(TeacherTeacherHead) 牛顿的各科成绩和总成绩(Totalscore).
成绩出来后,各科老师都得到通知(Notify).
三.具体编码
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.成绩单(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.语文老师
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.英语老师
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.数学老师
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.班主任
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 + 分); } } }
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.运行结果
9.总结
应用C#语言提供的事件和通知,可以让观察者模式更加优雅的实现。事件的 +=操作,实在是让人So happy。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。