>  기사  >  백엔드 개발  >  C#의 Observer 패턴은 뉴턴의 아동화 문제를 어떻게 해결합니까?

C#의 Observer 패턴은 뉴턴의 아동화 문제를 어떻게 해결합니까?

黄舟
黄舟원래의
2017-09-05 15:03:111443검색

이 글에서는 뉴턴 아동화 문제를 해결하기 위한 C# 디자인 패턴 중 옵저버 패턴을 주로 소개하고, 옵저버 패턴의 원리를 간략하게 설명하고, 뉴턴 아동화 문제를 해결하기 위해 옵저버 패턴을 활용하는 구체적인 단계와 관련 동작을 분석합니다. 구체적인 예제를 기반으로 한 신발이며 독자들이 다운로드하여 참조할 수 있는 데모 소스 코드가 함께 제공됩니다. 필요한 친구가 참조할 수 있습니다.

이 기사에서는 뉴턴의 문제를 해결하기 위해 C# 디자인 패턴에서 Observer 패턴의 예를 설명합니다. 어린이 신발. 참고할 수 있도록 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.

1. 이론적 정의

관찰자 패턴은 일대다 관계를 설명합니다. 객체의 상태가 변경되면 다른 객체에 변경 사항이 통보됩니다. 그리고 그에 따라 반응하세요.

2. 적용 예시

요구 사항 설명: 뉴턴 학생들의 기말고사 점수(점수)가 나왔는데, 전 과목 선생님들이 학생들의 점수를 알고 싶어 합니다!
중국어 선생님(Teacher Chinese)은 뉴턴의 중국어(중국어) 점수에만 관심이 있습니다.
영어 선생님(TeacherEnglish)은 뉴턴의 영어(영어) 점수에만 관심이 있습니다.
수학 선생님(TeacherMathematics)은 뉴턴의 수학(수학) 점수에만 관심이 있습니다.
담임선생님(TeacherTeacherHead)뉴튼의 과목별 점수와 총점(TotalScore)을 신경쓰고 싶습니다.
결과가 나온 후 각 과목의 선생님께 통보(Notify)합니다.

특정코딩 3.

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.점수(점수)


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

6 . 담임 선생님


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

7. 주요 함수 호출


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

8. 실행 결과


9.Summary

C# 언어에서 제공하는 이벤트와 알림을 적용하면 관찰자 모드가 됩니다. 더 우아한 구현. += 이벤트 운영이 정말 너무 행복해요.

위 내용은 C#의 Observer 패턴은 뉴턴의 아동화 문제를 어떻게 해결합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.