欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

一.理論定義

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

二.應用舉例

需求描述:牛頓同學的期末考試成績(Score)出來了,各科老師都想知道自己的 學生 成績情況!
語文老師(TeacherChinese)只關心  牛頓的語文(Chinese)成績.
英語老師(TeacherEnglish)只關心  牛頓的英語(English)成績.
數(shù)學老師(TeacherMathematics)只關心  牛頓的數(shù)學(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>
  /// 數(shù)學成績
  /// </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.數(shù)學老師

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Observer
{
 /// <summary>
 /// 數(shù)學老師
 /// </summary>
 public class TeacherMathematics
 {
  public void Receive(Score score) {
   Console.WriteLine("我是數(shù)學老師,我只關心" + score.student.Name + "的數(shù)學成績:" + 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 + "的 數(shù)學 成績: " + score.Mathematics + " 分");
   Console.WriteLine(name + "的 總 成績: " + score.TotalScore + " 分");
  }
 }
}

7.下面是主函數(shù)調用

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();//數(shù)學老師
   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);
   //向 各 學科 老師發(fā)送 有針對性的,感興趣的 成績
   score.Notify();
   #endregion
   Console.ReadKey();
  }
 }
}

8.運行結果

9.總結

應用C#語言提供的事件和通知,可以讓觀察者模式更加優(yōu)雅的實現(xiàn)。事件的 +=操作,實在是讓人So happy。

附:完整實例代碼點擊此處本站下載

更多關于C#相關內(nèi)容還可查看本站專題:《C#數(shù)據(jù)結構與算法教程》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數(shù)組操作技巧總結》及《C#面向對象程序設計入門教程

希望本文所述對大家C#程序設計有所幫助。

相關文章

  • c#?復寫Equals方法的實現(xiàn)

    c#?復寫Equals方法的實現(xiàn)

    本文主要介紹了c#?復寫Equals方法的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • c#數(shù)據(jù)綁定之向查詢中添加參數(shù)(.Net連接外部數(shù)據(jù)庫)

    c#數(shù)據(jù)綁定之向查詢中添加參數(shù)(.Net連接外部數(shù)據(jù)庫)

    本實例主要練習了ADO.Net連接到外部數(shù)據(jù)庫的基礎上,向查詢中添加參數(shù)。使用的是ACCESS數(shù)據(jù)庫
    2014-04-04
  • C#生成隨機驗證碼代碼分享

    C#生成隨機驗證碼代碼分享

    這篇文章主要分享了C#生成隨機驗證碼代碼,另外附上使用示例,非常的簡單實用,有需要的朋友可以參考下
    2014-10-10
  • C#實現(xiàn)掃雷游戲

    C#實現(xiàn)掃雷游戲

    這篇文章主要為大家詳細介紹了C#實現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C#異常處理知識匯總

    C#異常處理知識匯總

    這篇文章主要介紹了C#異常處理的知識,文中講解非常詳細,配合代碼幫助大家更好的理解,感興趣的朋友可以參考下
    2020-06-06
  • 基于C#實現(xiàn)亂碼視頻效果

    基于C#實現(xiàn)亂碼視頻效果

    亂碼視頻效果可能很多人都在抖音看到過,即把一個短視頻,轉成數(shù)字、字母等亂碼組成的形式進行播放。本文將用C#實現(xiàn)一下這一效果,感興趣的可以了解一下
    2023-01-01
  • C#特性之匿名方法和Lambda表達式

    C#特性之匿名方法和Lambda表達式

    這篇文章主要介紹了C#特性之匿名方法和Lambda表達式,需要的朋友可以參考下
    2014-12-12
  • C#實現(xiàn)標題閃爍效果的示例代碼

    C#實現(xiàn)標題閃爍效果的示例代碼

    在Windows系統(tǒng)中,當程序在后臺運行時,如果某個窗體的提示信息需要用戶瀏覽,該窗體就會不停地閃爍,這樣就會吸引用戶的注意,下面我們就來看看如何使用C#實現(xiàn)這一效果吧
    2024-04-04
  • Unity 如何獲取鼠標停留位置下的物體

    Unity 如何獲取鼠標停留位置下的物體

    這篇文章主要介紹了Unity 如何獲取鼠標停留位置下的物體,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 解析XPath語法之在C#中使用XPath的示例詳解

    解析XPath語法之在C#中使用XPath的示例詳解

    本篇文章是對在C#中使用XPath的示例進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05

最新評論