C#設(shè)計(jì)模式之Observer觀察者模式解決牛頓童鞋成績(jī)問(wèn)題示例
本文實(shí)例講述了C#設(shè)計(jì)模式之Observer觀察者模式解決牛頓童鞋成績(jī)問(wèn)題。分享給大家供大家參考,具體如下:
一.理論定義
觀察者模式 描述了 一種 一對(duì)多的關(guān)系。 當(dāng)某一對(duì)象的狀態(tài)發(fā)生改變時(shí),其他對(duì)象會(huì)得到 改變的通知。并作出相應(yīng)的反應(yīng)。
二.應(yīng)用舉例
需求描述:牛頓同學(xué)的期末考試成績(jī)(Score)出來(lái)了,各科老師都想知道自己的 學(xué)生 成績(jī)情況!
語(yǔ)文老師(TeacherChinese)只關(guān)心 牛頓的語(yǔ)文(Chinese)成績(jī).
英語(yǔ)老師(TeacherEnglish)只關(guān)心 牛頓的英語(yǔ)(English)成績(jī).
數(shù)學(xué)老師(TeacherMathematics)只關(guān)心 牛頓的數(shù)學(xué)(Mathematics)成績(jī).
班主任想關(guān)心(TeacherTeacherHead) 牛頓的各科成績(jī)和總成績(jī)(TotalScore).
成績(jī)出來(lái)后,各科老師都得到通知(Notify).
三.具體編碼
1.添加學(xué)生信息類,里面只有一個(gè)Name屬性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Observer
{
/// <summary>
/// 學(xué)生信息類
/// </summary>
public class Student
{
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
}
}
2.成績(jī)單(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>
/// 調(diào)用入口
/// </summary>
public void Notify() {
OnNotifyChange();
}
/// <summary>
///通知事件
/// </summary>
private void OnNotifyChange() {
if (NotifyEvent != null) {
NotifyEvent(this);
}
}
/// <summary>
/// 數(shù)學(xué)成績(jī)
/// </summary>
public float Mathematics { get; set; }
/// <summary>
/// 英語(yǔ)成績(jī)
/// </summary>
public float English { get; set; }
/// <summary>
/// 語(yǔ)文成績(jī)
/// </summary>
public float Chinese { get; set; }
/// <summary>
/// 三科總成績(jī)
/// </summary>
public float TotalScore {
get {
return Mathematics+English+Chinese;
}
}
/// <summary>
/// 學(xué)生基本信息
/// </summary>
public Student student { get; set; }
}
}
3.語(yǔ)文老師
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Observer
{
/// <summary>
/// 語(yǔ)文老師
/// </summary>
public class TeacherChinese
{
public void Receive(Score score) {
Console.WriteLine("我是語(yǔ)文老師,我只關(guān)心"+score.student.Name+"的語(yǔ)文成績(jī):"+score.Chinese+"分");
}
}
}
4.英語(yǔ)老師
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Observer
{
/// <summary>
/// 英語(yǔ)老師
/// </summary>
public class TeacherEnglish
{
public void Receive(Score score) {
Console.WriteLine("我是英語(yǔ)老師,我只關(guān)心" + score.student.Name + "的英語(yǔ)成績(jī):" + score.English + "分");
}
}
}
5.數(shù)學(xué)老師
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Observer
{
/// <summary>
/// 數(shù)學(xué)老師
/// </summary>
public class TeacherMathematics
{
public void Receive(Score score) {
Console.WriteLine("我是數(shù)學(xué)老師,我只關(guān)心" + score.student.Name + "的數(shù)學(xué)成績(jī):" + 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("我是班主任,我要關(guān)心 " + name + " 的各科成績(jī)和總成績(jī)");
Console.WriteLine(name + "的 語(yǔ)文 成績(jī): " + score.Chinese + " 分");
Console.WriteLine(name + "的 英語(yǔ) 成績(jī): " + score.English + " 分");
Console.WriteLine(name + "的 數(shù)學(xué) 成績(jī): " + score.Mathematics + " 分");
Console.WriteLine(name + "的 總 成績(jī): " + score.TotalScore + " 分");
}
}
}
7.下面是主函數(shù)調(diào)用
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
/*牛頓同學(xué)的成績(jī)單*/
Score score = new Score {
Chinese = 60,
Mathematics = 100,
English = 90,
student = new Student { Name = "牛頓" }
};
TeacherChinese teacherChinese = new TeacherChinese(); //語(yǔ)文老師
TeacherEnglish teacherEnglish = new TeacherEnglish();//英語(yǔ)老師
TeacherMathematics teacherMathematics = new TeacherMathematics();//數(shù)學(xué)老師
TeacherTeacherHead teacherTeacherHead = new TeacherTeacherHead();//班主任
//牛頓成績(jī)單出來(lái)了,老師都想知道這個(gè)結(jié)果。
score.NotifyEvent += new NotifyEventHandler(teacherChinese.Receive);
score.NotifyEvent += new NotifyEventHandler(teacherEnglish.Receive);
score.NotifyEvent += new NotifyEventHandler(teacherMathematics.Receive);
score.NotifyEvent += new NotifyEventHandler(teacherTeacherHead.Receive);
//向 各 學(xué)科 老師發(fā)送 有針對(duì)性的,感興趣的 成績(jī)
score.Notify();
#endregion
Console.ReadKey();
}
}
}
8.運(yùn)行結(jié)果
9.總結(jié)
應(yīng)用C#語(yǔ)言提供的事件和通知,可以讓觀察者模式更加優(yōu)雅的實(shí)現(xiàn)。事件的 +=操作,實(shí)在是讓人So happy。
附:完整實(shí)例代碼點(diǎn)擊此處本站下載。
更多關(guān)于C#相關(guān)內(nèi)容還可查看本站專題:《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#窗體操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
上一篇:C# 文件上傳下載(Excel導(dǎo)入,多線程下載)功能的實(shí)現(xiàn)代碼
欄 目:C#教程
下一篇:C#單例模式(Singleton Pattern)詳解
本文標(biāo)題:C#設(shè)計(jì)模式之Observer觀察者模式解決牛頓童鞋成績(jī)問(wèn)題示例
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5515.html
您可能感興趣的文章
- 01-10深入淺出23種設(shè)計(jì)模式
- 01-10C#一個(gè)簡(jiǎn)單的定時(shí)小程序?qū)崿F(xiàn)代碼
- 01-10微信開(kāi)放平臺(tái)之網(wǎng)站授權(quán)微信登錄功能
- 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量二
- 01-10C#編程自學(xué)之開(kāi)篇介紹
- 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量三
- 01-10C#編程自學(xué)之運(yùn)算符和表達(dá)式
- 01-10C#編程自學(xué)之類和對(duì)象
- 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量一
- 01-10C#編程自學(xué)之流程控制語(yǔ)句


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什


