ホームページ  >  記事  >  バックエンド開発  >  C# の Observer パターンがニュートンの子供の靴の問題を解決する方法の例

C# の Observer パターンがニュートンの子供の靴の問題を解決する方法の例

黄舟
黄舟オリジナル
2017-09-02 14:08:421189ブラウズ

この記事では、ニュートンの子供の靴の問題を解決するための C# 設計パターンのオブザーバー パターンを主に紹介し、オブザーバー パターンの原理を簡単に説明し、オブザーバー パターンを使用してニュートンの子供の靴の問題を解決する具体的な手順と関連操作を分析します。具体的な例に基づいたシューズのテクニックを紹介しており、読者がダウンロードして参照できるデモ ソース コードが付属しています。この記事では、ニュートンの問題を解決するための C# 設計パターンの Observer パターンの例について説明します。子供用の靴。参考のために皆さんと共有してください。詳細は次のとおりです:

1. 理論的定義

オブザーバー パターンは 1 対多の関係を表します。 オブジェクトの状態が変化すると、他のオブジェクトにその変化が通知されます。そしてそれに応じて反応します。

2. 応用例

要件の説明: Newton の生徒の期末試験のスコア (Score) が出てきて、すべての教科の教師が生徒のスコアを知りたいと考えています。

中国語の先生 (TeacherEnglish) はニュートンの中国語 (中国語) のスコアのみを気にします。

英語の先生 (TeacherEnglish) はニュートンの英語 (英語) のスコアのみを気にします。
数学の先生 (TeacherMathematics) はニュートンの数学 (数学) のスコアのみを気にします。
気をつけたい担任の先生(TeacherTeacherHead) 各教科のニュートンの得点と合計点(TotalScore)
結果が出たら各教科の先生に通知(Notify)

具体的なコーディング

3つ。 1. 学生情報クラスを追加します。 Name プロパティは 1 つだけです。

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. スコア(スコア)

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; }
 }
}

4. 英語の先生

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+"分");
  }
 }
}

5.

りー

6 .クラスの先生


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 + "分");
  }
 }
}

7. 以下はメイン関数呼び出しです


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 + "分");
  }
 }
}

8. 実行結果


9. C# 言語によって提供されるイベントと通知を使用すると、オブザーバー モードの詳細 エレガントな実装。イベントの += 操作は本当に嬉しいです。

以上がC# の Observer パターンがニュートンの子供の靴の問題を解決する方法の例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。