一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - C#設計模式之Observer觀察者模式解決牛頓童鞋成績問題示例

C#設計模式之Observer觀察者模式解決牛頓童鞋成績問題示例

2022-01-21 13:41GhostRider C#

這篇文章主要介紹了C#設計模式之Observer觀察者模式解決牛頓童鞋成績問題,簡單講述了觀察者模式的原理并結合具體實例形式分析了使用觀察者模式解決牛頓童鞋成績問題的具體步驟相關操作技巧,并附帶demo源碼供讀者下載參考,需要

本文實例講述了C#設計模式之Observer觀察者模式解決牛頓童鞋成績問題。分享給大家供大家參考,具體如下:

一.理論定義

觀察者模式 描述了 一種 一對多的關系。 當某一對象的狀態發生改變時,其他對象會得到 改變的通知。并作出相應的反應。

二.應用舉例

需求描述:牛頓同學的期末考試成績(Score)出來了,各科老師都想知道自己的 學生 成績情況!
語文老師(TeacherChinese)只關心  牛頓的語文(Chinese)成績.
英語老師(TeacherEnglish)只關心  牛頓的英語(English)成績.
數學老師(TeacherMathematics)只關心  牛頓的數學(Mathematics)成績.
班主任想關心(TeacherTeacherHead)    牛頓的各科成績和總成績(TotalScore).
成績出來后,各科老師都得到通知(Notify).

三.具體編碼

1.添加學生信息類,里面只有一個Name屬性。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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.語文老師

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.英語老師

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.數學老師

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.班主任

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.下面是主函數調用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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#程序設計有所幫助。

原文鏈接:http://www.cnblogs.com/HCCZX/archive/2012/07/30/2615018.html

延伸 · 閱讀

精彩推薦
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
主站蜘蛛池模板: 黄色大片网 | 情人我吃糖果小说 | 护士让我吃奶我扒她奶 | 欧美伊人影院 | 精品久久伦理中文字幕 | 8x8拨擦拨擦华人免费 | 高清一区二区 | 日本高清色视频www 日本高清免费观看 | 拿捏小说| 91大神在线精品播放 | 久久精品国产只有精品 | 美女天天操 | 亚洲精品在线免费观看视频 | 亚洲色图首页 | 51精品 | www.色女人.com | 亚洲视频在线免费 | 国产在视频线精品视频 | 18xxxx中国| 日本激情网 | 98成人网| 日韩porn | 四虎永久在线精品波多野结衣 | 色哟哟国产成人精品 | 亚洲精品国产在线 | 欧美黑人ⅹxxx片 | 女教师巨大乳孔中文字幕免费 | 99 久久99久久精品免观看 | 日本综合在线观看 | 日本高清视频在线免费观看 | 色综合伊人色综合网亚洲欧洲 | 国产资源视频在线观看 | 色综合伊人色综合网站中国 | 色老板美国在线观看 | 色橹| 黑人同学征服教师麻麻 | 国产成人福利免费视频 | 精品麻豆国产 | 西西人体大胆啪啪私拍色约约 | 明星ai智能人脸替换造梦在线播放 | 插入逼|