当前位置: 移动技术网 > IT编程>开发语言>c# > C#设计模式之Observer观察者模式解决牛顿童鞋成绩问题示例

C#设计模式之Observer观察者模式解决牛顿童鞋成绩问题示例

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#设计模式之observer观察者模式解决牛顿童鞋成绩问题。分享给大家供大家参考,具体如下: 一.理论定义 观察者模式 描述了 一种 一对多的关系。 当

本文实例讲述了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#相关内容还可查看本站专题:《c#数据结构与算法教程》、《c#窗体操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网