C \ # 이벤트 의뢰 (2)

59295 단어 C#

  
    
Observer
, 。 : 、 、 , 。 , , ; 、 。
, :
//
public class Heater {
private int temperature;

//
private void BoilWater() {
for ( int i = 0 ; i <= 100 ; i ++ ) {
temperature
= i;
}
}
}

//
public class Alarm{
private void MakeAlert( int param) {
Console.WriteLine(
" Alarm: , {0} : " , param);
}
}

//
public class Display{
private void ShowMsg( int param) {
Console.WriteLine(
" Display: , :{0} 。 " , param);
}
}
: ? , Observer ,Observer :
1 . Subject: , 。 , , , temprature , 100 , 。
2 . Observer: , Subject, Subject , Observer, Observer 。 ,Observer , 。
, :
1 . , ( )。
2 . 。
3 . , 95 , , MakeAlert() 、 ShowMsg() 。
,GOF , Observer :Observer , , 。Observer 。
Observer
, , , 。
using System;
using System.Collections.Generic;
using System.Text;

namespace Delegate {
//
public class Heater {
private int temperature;
public delegate void BoilHandler( int param); //
public event BoilHandler BoilEvent; //

//
public void BoilWater() {
for ( int i = 0 ; i <= 100 ; i ++ ) {
temperature
= i;

if (temperature > 95 ) {
if (BoilEvent != null ) { //
BoilEvent(temperature); //
}
}
}
}
}

//
public class Alarm {
public void MakeAlert( int param) {
Console.WriteLine(
" Alarm: , {0} : " , param);
}
}

//
public class Display {
public static void ShowMsg( int param) { //
Console.WriteLine( " Display: , :{0} 。 " , param);
}
}

class Program {
static void Main() {
Heater heater
= new Heater();
Alarm alarm
= new Alarm();

heater.BoilEvent
+= alarm.MakeAlert; //
heater.BoilEvent += ( new Alarm()).MakeAlert; //
heater.BoilEvent += Display.ShowMsg; //

heater.BoilWater();
//
}
}
}

Alarm: ,
96
Alarm: ,
96
Display: , :96 。
// ...
.Net Framework
, : .Net Framework ? EventArgs ?
, .Net Framework :
? EventHandler 。
? : void , : Object , EventArgs ( EventArgs)。
? EventHandler 。
? EventArgs EventArgs 。

1 . Object Subject, , Heater( )。 ( Alarm MakeAlert) (Heater)。
2 . EventArgs Observer , temperature。
, 。 , , Observer ( ) 、 、 , , , 。
, .Net Framework :
using System;
using System.Collections.Generic;
using System.Text;

namespace Delegate {
//
public class Heater {
private int temperature;
public string type = " RealFire 001 " ; //
public string area = " China Xian " ; //
//
public delegate void BoiledEventHandler(Object sender, BoiledEventArgs e);
public event BoiledEventHandler Boiled; //

// BoiledEventArgs , Observer
public class BoiledEventArgs : EventArgs {
public readonly int temperature;
public BoiledEventArgs( int temperature) {
this .temperature = temperature;
}
}

// Heater ,
protected virtual void OnBoiled(BoiledEventArgs e) {
if (Boiled != null ) { //
Boiled( this , e); //
}
}

//
public void BoilWater() {
for ( int i = 0 ; i <= 100 ; i ++ ) {
temperature
= i;
if (temperature > 95 ) {
// BoiledEventArgs 。
BoiledEventArgs e = new BoiledEventArgs(temperature);
OnBoiled(e);
// OnBolied
}
}
}
}

//
public class Alarm {
public void MakeAlert(Object sender, Heater.BoiledEventArgs e) {
Heater heater
= (Heater)sender; //
// sender
Console.WriteLine( " Alarm:{0} - {1}: " , heater.area, heater.type);
Console.WriteLine(
" Alarm: , {0} : " , e.temperature);
Console.WriteLine();
}
}

//
public class Display {
public static void ShowMsg(Object sender, Heater.BoiledEventArgs e) { //
Heater heater = (Heater)sender;
Console.WriteLine(
" Display:{0} - {1}: " , heater.area, heater.type);
Console.WriteLine(
" Display: , :{0} 。 " , e.temperature);
Console.WriteLine();
}
}

class Program {
static void Main() {
Heater heater
= new Heater();
Alarm alarm
= new Alarm();

heater.Boiled
+= alarm.MakeAlert; //
heater.Boiled += ( new Alarm()).MakeAlert; //
heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert); //
heater.Boiled += Display.ShowMsg; //

heater.BoilWater();
//
}
}
}


Alarm:China Xian
- RealFire 001 :
Alarm: ,
96
Alarm:China Xian
- RealFire 001 :
Alarm: ,
96
Alarm:China Xian
- RealFire 001 :
Alarm: ,
96
Display:China Xian
- RealFire 001 :
Display: , :96 。
// ...

GreetingPeople 、 , , 。
, Observer , , .Net Framework 、 。

C# ( )

C# , 。 , 。 , , 、 、 。

C# , , 。 , , , ( ) 。 ? :
NOTE: , , (publisher)、 (subscriber)、 (client)。 Observer , (subject) (observer)。 Main() Program 。
class Program {
static void Main( string [] args) {
Publishser pub
= new Publishser();
Subscriber sub
= new Subscriber();

pub.NumberChanged
+= new NumberChangedEventHandler(sub.OnNumberChanged);
pub.DoSomething();
// DoSomething()
pub.NumberChanged( 100 ); //
}
}

//
public delegate void NumberChangedEventHandler( int count);

//
public class Publishser {
private int count;
public NumberChangedEventHandler NumberChanged; //
// public event NumberChangedEventHandler NumberChanged; //

public void DoSomething() {
// ...

if (NumberChanged != null ) { //
count ++ ;
NumberChanged(count);
}
}
}

//
public class Subscriber {
public void OnNumberChanged( int count) {
Console.WriteLine(
" Subscriber notified: count = {0} " , count);
}
}
NumberChangedEventHandler , Publisher Subscriber。 , , pub.NumberChanged(
100 ), 。 , DoSomething() 。 event , , , ( pub.NumberChanged( 100 ) ), Publisher ( pub.DoSomething() ), , NumberChanged( 100 ) Publisher 。
, , 。 , event , , pub.NumberChanged(
100 ), 。 DoSomething() 。 , 。
, int object , , 。 , , , , 。
NOTE: , , “On ”, OnNumberChanged。
void?
, void, ? , , , , , 。 。 , , 、 , , 。
class Program {
static void Main( string [] args) {
Publishser pub
= new Publishser();
Subscriber1 sub1
= new Subscriber1();
Subscriber2 sub2
= new Subscriber2();
Subscriber3 sub3
= new Subscriber3();

pub.NumberChanged
+= new GeneralEventHandler(sub1.OnNumberChanged);
pub.NumberChanged
+= new GeneralEventHandler(sub2.OnNumberChanged);
pub.NumberChanged
+= new GeneralEventHandler(sub3.OnNumberChanged);
pub.DoSomething();
//
}
}

//
public delegate string GeneralEventHandler();

//
public class Publishser {
public event GeneralEventHandler NumberChanged; //
public void DoSomething() {
if (NumberChanged != null ) { //
string rtn = NumberChanged();
Console.WriteLine(rtn);
// , Subscriber3
}
}
}

//
public class Subscriber1 {
public string OnNumberChanged() {
return " Subscriber1 " ;
}
}
public class Subscriber2 { /* , , Subscriber2 */ }
public class Subscriber3 { /* , , Subscriber3 */ }
, Subscriber3, , 。

, , “ ” ( , ), 。 ? , private , :
//
public class Publishser {
private event GeneralEventHandler NumberChanged; //
//
public void Register(GeneralEventHandler method) {
NumberChanged
= method;
}
//
public void UnRegister(GeneralEventHandler method) {
NumberChanged
-= method;
}

public void DoSomething() {
//
if (NumberChanged != null ) { //
string rtn = NumberChanged();
Console.WriteLine(
" Return: {0} " , rtn); // , Subscriber3
}
}
}
NOTE: , UnRegister() , NumberChanged
-= method 。 method , , , 。
Register() , “
= ”, “ += ”, 。 , :
1 、 NumberChanged , , , , 、 。 Register() UnRegister() , DoSomething() ( NumberChanged , )。
2 、 , 、 NumberChanged C# ? , C# , 。 , , ? ,C# (Event Accessor) , 。 :
class Program {
static void Main( string [] args) {
Publishser pub
= new Publishser();
Subscriber1 sub1
= new Subscriber1();
Subscriber2 sub2
= new Subscriber2();

pub.NumberChanged
-= sub1.OnNumberChanged; //
pub.NumberChanged += sub2.OnNumberChanged; // sub2
pub.NumberChanged += sub1.OnNumberChanged; // sub1 sub2

pub.DoSomething();
//
}
}

//
public delegate string GeneralEventHandler();

//
public class Publishser {
//
private GeneralEventHandler numberChanged;
//
public event GeneralEventHandler NumberChanged {
add {
numberChanged
= value;
}
remove {
numberChanged
-= value;
}
}

public void DoSomething() {
//
if (numberChanged != null ) { //
string rtn = numberChanged();
Console.WriteLine(
" Return: {0} " , rtn); //
}
}
}

//
public class Subscriber1 {
public string OnNumberChanged() {
Console.WriteLine(
" Subscriber1 Invoked! " );
return " Subscriber1 " ;
}
}
public class Subscriber2 { /* */ }
public class Subscriber3 { /* */ }
public
event GeneralEventHandler NumberChanged {add{...}remove{...}} 。 , DoSomething numberChanged , NumberChanged ( ) , 。 :
Subscriber1 Invoked
!
Return: Subscriber1

좋은 웹페이지 즐겨찾기