最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#設(shè)計(jì)模式之Observer觀察者模式解決牛頓童鞋成績問題示例

 更新時(shí)間:2017年09月01日 12:03:13   作者:GhostRider  
這篇文章主要介紹了C#設(shè)計(jì)模式之Observer觀察者模式解決牛頓童鞋成績問題,簡單講述了觀察者模式的原理并結(jié)合具體實(shí)例形式分析了使用觀察者模式解決牛頓童鞋成績問題的具體步驟相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下

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

一.理論定義

觀察者模式 描述了 一種 一對多的關(guān)系。 當(dāng)某一對象的狀態(tài)發(fā)生改變時(shí),其他對象會(huì)得到 改變的通知。并作出相應(yīng)的反應(yīng)。

二.應(yīng)用舉例

需求描述:牛頓同學(xué)的期末考試成績(Score)出來了,各科老師都想知道自己的 學(xué)生 成績情況!
語文老師(TeacherChinese)只關(guān)心  牛頓的語文(Chinese)成績.
英語老師(TeacherEnglish)只關(guān)心  牛頓的英語(English)成績.
數(shù)學(xué)老師(TeacherMathematics)只關(guān)心  牛頓的數(shù)學(xué)(Mathematics)成績.
班主任想關(guān)心(TeacherTeacherHead)    牛頓的各科成績和總成績(TotalScore).
成績出來后,各科老師都得到通知(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.成績單(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é)成績
  /// </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>
  /// 學(xué)生基本信息
  /// </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("我是語文老師,我只關(guān)心"+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("我是英語老師,我只關(guān)心" + score.student.Name + "的英語成績:" + 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é)成績:" + 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 + " 的各科成績和總成績");
   Console.WriteLine(name + "的 語文 成績: " + score.Chinese + " 分");
   Console.WriteLine(name + "的 英語 成績: " + score.English + " 分");
   Console.WriteLine(name + "的 數(shù)學(xué) 成績: " + score.Mathematics + " 分");
   Console.WriteLine(name + "的 總 成績: " + 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é)的成績單*/
   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();//數(shù)學(xué)老師
   TeacherTeacherHead teacherTeacherHead = new TeacherTeacherHead();//班主任
   //牛頓成績單出來了,老師都想知道這個(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ā)送 有針對性的,感興趣的 成績
   score.Notify();
   #endregion
   Console.ReadKey();
  }
 }
}

8.運(yùn)行結(jié)果

9.總結(jié)

應(yīng)用C#語言提供的事件和通知,可以讓觀察者模式更加優(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#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程

希望本文所述對大家C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C#使用EF連接PGSql數(shù)據(jù)庫的完整步驟

    C#使用EF連接PGSql數(shù)據(jù)庫的完整步驟

    這篇文章主要給大家介紹了關(guān)于C#使用EF連接PGSql數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • C#實(shí)現(xiàn)文件讀寫到SQLite數(shù)據(jù)庫

    C#實(shí)現(xiàn)文件讀寫到SQLite數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了使用?C#?將文件讀寫到?SQLite?數(shù)據(jù)庫的幾種方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2025-01-01
  • C# EF去除重復(fù)列DistinctBy方式

    C# EF去除重復(fù)列DistinctBy方式

    這篇文章主要介紹了C# EF去除重復(fù)列DistinctBy方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 詳解C# 枚舉高級用法之Description

    詳解C# 枚舉高級用法之Description

    這篇文章主要介紹了C# 枚舉高級用法之Description,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • C# 使用相同權(quán)限調(diào)用 cmd 傳入命令的方法

    C# 使用相同權(quán)限調(diào)用 cmd 傳入命令的方法

    本文告訴大家如何使用相同權(quán)限調(diào)用cmd并且傳入命令,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-07-07
  • C# Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例

    C# Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例

    本文主要介紹了C# Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Unity3D Shader實(shí)現(xiàn)動(dòng)態(tài)屏幕遮罩

    Unity3D Shader實(shí)現(xiàn)動(dòng)態(tài)屏幕遮罩

    這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)動(dòng)態(tài)屏幕遮罩效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#橋接模式完整實(shí)例

    C#橋接模式完整實(shí)例

    這篇文章主要介紹了C#橋接模式,以實(shí)例形式較為詳細(xì)的分析了C#橋接模式的實(shí)現(xiàn)原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • C# GetField方法的應(yīng)用實(shí)例講解

    C# GetField方法的應(yīng)用實(shí)例講解

    C#中的GetField是一個(gè)反射方法,用于獲取指定類型的字段信息,它可以通過字段名稱來獲取字段對象,并且可以在運(yùn)行時(shí)動(dòng)態(tài)地訪問和操作這些字段,本文給大家介紹了C# GetField方法的應(yīng)用,需要的朋友可以參考下
    2024-04-04
  • Unity3D實(shí)現(xiàn)旋鈕控制燈光效果

    Unity3D實(shí)現(xiàn)旋鈕控制燈光效果

    這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)旋鈕控制燈光效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04

最新評論

岗巴县| 伊金霍洛旗| 大丰市| 苏尼特左旗| 浏阳市| 九江县| 白水县| 宁海县| 武隆县| 凤城市| 丹东市| 甘德县| 资阳市| 廉江市| 保山市| 普安县| 拉孜县| 大理市| 舟山市| 乡城县| 托里县| 南郑县| 会昌县| 连江县| 大宁县| 安岳县| 闸北区| 呼和浩特市| 同德县| 内江市| 万载县| 长顺县| 宜黄县| 宜君县| 曲沃县| 即墨市| 滦南县| 苏州市| 岳池县| 邵东县| 安平县|