首頁  >  文章  >  後端開發  >  C#中Observer觀察者模式解決牛頓童鞋成績問題的實例

C#中Observer觀察者模式解決牛頓童鞋成績問題的實例

黄舟
黄舟原創
2017-09-02 14:08:421189瀏覽

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

本文實例講述了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#中Observer觀察者模式解決牛頓童鞋成績問題的實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn