C # Implementation Observer Observer mode

Source: Internet
Author: User
Tags instance method

namespace Observer
{
//defines a delegate that defines the signature of the Observer method, which is a protocol bar
public delegate void Numbereventhandler ( Object Sender,numbereventargs e);
//What parameters are passed to the observer? In this definition, note that you want to inherit from EventArgs
public class Numbereventargs:eventargs
{
Public numbereventargs (int number)
{ _number = number;  
}
Private int _number;
public int number
{
get {_number;}
Set {_number = value;}
}
}
//Subject in Observer Mode
public class Subject
{
//define an event, which is the instance of the delegate
public event Numbereventhandler Nu mberreached;
public void Dowithloop (int number)
{
for (int i = 0; I <= i++)
{
//trigger event conditions to
if (    i = = Number
{
Numbereventargs e = new Numbereventargs (i);
Onnumberreached (e);
}
}
}
//Note that this method is defined as protected, virtual, and can be overridden on behalf of subclasses, altering the behavior of triggering events
//Can not even trigger events protected virtual void Onnumberreach Ed (Numbereventargs e)
{
//To determine whether the event is null, that is, whether the method is bound to
if (numberreached!= null) NumbErreached (this, e);
}
}
public class Mainprogram
{
public static void Main ()
{
Console.WriteLine ("Please Input a 0-100 number: ");
int input = Console.read ();
if (Input < 0 | | | input >) {Console.WriteLine ("Error");}
Subject s = new Subject ();  
//To the event binding method, static
S.numberreached + = new Numbereventhandler (msgbox_numberreached);
Mainprogram MP = new Mainprogram ();  
//To the event binding method, instance method
S.numberreached + = new Numbereventhandler (mp.console_numberreached);
S.dowithloop (input); Console.read ();
}
void Console_numberreached (object sender, Numbereventargs e)
{
Console.WriteLine (e.number.tostring ( ));
}
static void Msgbox_numberreached (object sender, Numbereventargs e)
{
MessageBox.Show (e.number.tost Ring ());
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.