本文实例报告了C#设想情势之Observer观察者情势处理牛顿童鞋效果题目。分享给人人供人人参考,细致以下:
一.理论定义
观察者情势 形貌了 一种 一对多的关联。 当某一对象的状况发作转变时,其他对象会获得 转变的关照。并作出响应的回响反映。
二.运用举例
需求形貌:牛顿同砚的期末考试效果(Score)出来了,各科先生都想晓得本身的 门生 效果状况!
语文先生(TeacherChinese)只体贴 牛顿的语文(Chinese)效果.
英语先生(TeacherEnglish)只体贴 牛顿的英语(English)效果.
数学先生(TeacherMathematics)只体贴 牛顿的数学(Mathematics)效果.
班主任想体贴(TeacherTeacherHead) 牛顿的各科效果和总效果(TotalScore).
效果出来后,各科先生都获得关照(Notify).
三.细致编码
1.增加门生信息类,内里只要一个Name属性。
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 + " 分"); } } }
7.下面是主函数挪用
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。
以上就是C#中Observer观察者情势处理牛顿童鞋效果题目的实例的细致内容,更多请关注ki4网别的相干文章!