网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C#中Observer观察者模式牛顿童鞋成绩问题的怎么解决

这篇文章将为大家详细讲解有关C#中Observer观察者模式牛顿童鞋成绩问题的怎么解决,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

元氏网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。成都创新互联公司2013年至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司

这篇文章主要介绍了C#设计模式之Observer观察者模式解决牛顿童鞋成绩问题,简单讲述了观察者模式的原理并结合具体实例形式分析了使用观察者模式解决牛顿童鞋成绩问题的具体步骤相关操作技巧,并附带demo源码供读者下载参考,需要的朋友可以参考下

本文实例讲述了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
{
 /// 
 /// 学生信息类
 /// 
 public class Student
 {
  /// 
  /// 姓名
  /// 
  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;
  /// 
  /// 调用入口
  /// 
  public void Notify() {
   OnNotifyChange();
  }
  /// 
  ///通知事件
  /// 
  private void OnNotifyChange() {
   if (NotifyEvent != null) {
    NotifyEvent(this);
   }
  }
  /// 
  /// 数学成绩
  /// 
  public float Mathematics { get; set; }
  /// 
  /// 英语成绩
  /// 
  public float English { get; set; }
  /// 
  /// 语文成绩
  /// 
  public float Chinese { get; set; }
  /// 
  /// 三科总成绩
  /// 
  public float TotalScore {
   get {
    return Mathematics+English+Chinese;
   }
  }
  /// 
  /// 学生基本信息
  /// 
  public Student student { get; set; }
 }
}

3.语文老师

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Observer
{
 /// 
 /// 语文老师
 /// 
 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
{
 /// 
 /// 英语老师
 /// 
 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
{
 /// 
 /// 数学老师
 /// 
 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
{
 /// 
 /// 班主任
 /// 
 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.运行结果

C#中Observer观察者模式牛顿童鞋成绩问题的怎么解决

9.总结

应用C#语言提供的事件和通知,可以让观察者模式更加优雅的实现。事件的 +=操作,实在是让人So happy。

关于C#中Observer观察者模式牛顿童鞋成绩问题的怎么解决就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网页标题:C#中Observer观察者模式牛顿童鞋成绩问题的怎么解决
网址分享:http://bjjierui.cn/article/ppdhgd.html

其他资讯