本文实例讲述了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#程序设计有所帮助。
# C#
# 设计模式
# Observer
# 观察者模式
# 牛顿童鞋成绩问题
# C#中的EventHandler观察者模式详解
# c# 实现观察者模式
# c# 使用计时器和观察者模式实现报警推送需求
# C# 设计模式系列教程-观察者模式
# C#中观察者模式的3种实现方式
# C#设计模式之观察者模式实例讲解
# 一起来学习C#的观察者模式
# 我是
# 各科
# 英语
# 英语老师
# 我只
# 总成绩
# 都想
# 程序设计
# 自己的
# 操作技巧
# 我要
# 让人
# 出来了
# 相关内容
# 感兴趣
# 数据结构
# 给大家
# 只有一个
# 点击此处
# 还可
相关文章:
动图在线制作网站有哪些,滑动动图图集怎么做?
已有域名能否直接搭建网站?
如何通过老薛主机一键快速建站?
C++中的Pimpl idiom是什么,有什么好处?(隐藏实现)
高端网站建设与定制开发一站式解决方案 中企动力
b2c电商网站制作流程,b2c水平综合的电商平台?
如何在七牛云存储上搭建网站并设置自定义域名?
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
个人网站制作流程图片大全,个人网站如何注销?
建站之星与建站宝盒如何选择最佳方案?
临沂网站制作企业,临沂第三中学官方网站?
整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?
历史网站制作软件,华为如何找回被删除的网站?
如何快速搭建响应式可视化网站?
教程网站设计制作软件,怎么创建自己的一个网站?
建站之星手机一键生成:多端自适应+小程序开发快速建站指南
浙江网站制作公司有哪些,浙江栢塑信息技术有限公司定制网站做的怎么样?
宝盒自助建站智能生成技巧:SEO优化与关键词设置指南
相亲简历制作网站推荐大全,新相亲大会主持人小萍萍资料?
交易网站制作流程,我想开通一个网站,注册一个交易网址,需要那些手续?
如何在云服务器上快速搭建个人网站?
建站之星IIS配置教程:代码生成技巧与站点搭建指南
婚礼视频制作网站,学习*后期制作的网站有哪些?
如何零基础在云服务器搭建WordPress站点?
,如何利用word制作宣传手册?
网站图片在线制作软件,怎么在图片上做链接?
做企业网站制作流程,企业网站制作基本流程有哪些?
如何做静态网页,sublimetext3.0制作静态网页?
建站之星安装后界面空白如何解决?
建站之星多图banner生成与模板自定义指南
,交易猫的商品怎么发布到网站上去?
如何通过IIS搭建网站并配置访问权限?
建站之星体验版:智能建站系统+响应式设计,多端适配快速建站
制作销售网站教学视频,销售网站有哪些?
云南网站制作公司有哪些,云南最好的招聘网站是哪个?
如何自定义建站之星网站的导航菜单样式?
再谈Python中的字符串与字符编码(推荐)
javascript中的try catch异常捕获机制用法分析
如何快速搭建安全的FTP站点?
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
中山网站推广排名,中山信息港登录入口?
北京网站制作网页,网站升级改版需要多久?
建站之星代理如何优化在线客服效率?
如何快速生成橙子建站落地页链接?
山东云建站价格为何差异显著?
网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?
如何快速生成专业多端适配建站电话?
视频网站app制作软件,有什么好的视频聊天网站或者软件?
建站主机系统SEO优化与智能配置核心关键词操作指南
建站之星图片链接生成指南:自助建站与智能设计教程
*请认真填写需求信息,我们会在24小时内与您取得联系。