A question (C #) when a cat and a mouse wake up the host #)

Source: Internet
Author: User
Program Suggestion: All the mice started to escape, and the master was awakened. (C # Statement)
Requirements:
1. The behavior of a mouse and a host must be active.
2. The test token is scalable. the token is called Token, which may cause other dynamic effects.
Most of the answers are using event programming. I have changed my mind here, using the observation mode, and using the interface, because considering the second requirement, that is to say, the cat may also directly cause the host to wake up, so man also inherits the icatcatcher interface.
Source code As follows:
Using system;

Using system. collections;



Namespace Test

{



Public interface icatcatcher


{

Void dosomething ();


}



Public interface icatsubject


{

Void regesitercatcatcher (icatcatcher catcatcher );

Void miao ();


}



Public interface iratsubject


{

Void regesiterratcatcher (iratcatcher Ratcatcher );

Void run ();


}



Public interface iratcatcher


{

Void wake ();


}



Public class Cat: icatsubject


{

Public CAT ()


{


}


private arraylist catcherlist = new arraylist ();
Public void regesitercatcatcher (icatcatcher catcher)

{< br>
catcherlist. add (catcher);

}< br>
Public void miao ()

{< br>
console. writeline ("Miao");
for (INT I = 0; I
{< br>
icatcatcher catcatcatcher = (icatcatcher) catcherlist ;

catcatcher. dosomething ();

}< br>
[stathread]

Public static void main ()

{< br>
CAT = new CAT ();



Rat [] rat = new rat [10];

For (INT I = 0; I <10; I ++)


{


Rat=New rat (CAT );


}




Man man = new man (rat, CAT );




Cat. Miao ();


}


}



Public class rat: icatcatcher, iratsubject


{

Public rat (icatsubject catsub)


{


Catsub. regesitercatcatcher (this );


}



Public void dosomething ()


{


Run ();


}


Private arraylist ratcherlist = new arraylist ();

Public void regesiterratcatcher (iratcatcher catcher)


{


Ratcherlist. Add (catcher );


}



Public void run ()


{


Console. writeline ("rat run ");

For (INT I = 0; I <ratcherlist. Count; I ++)


{


Iratcatcher Ratcatcher = (iratcatcher) ratcherlist ;


Ratcatcher. Wake ();


}


}


}



Public class man: icatcatcher, iratcatcher


{

Public man (iratsubject [] ratsub, icatsubject catsub)


{

For (INT I = 0; I <ratsub. length; I ++)


{


Ratsub . Regesiterratcatcher ( This );


}


Catsub. regesitercatcatcher (this );


}



Public void dosomething ()


{


Console. writeline ("cat bring on wake ");


}


Public void wake ()


{


Console. writeline ("rats bring on wake ");


}


}

}

If debugging occurs, there will be a small problem, that is, there are many mice, and each mouse's run will have a wake man, so the host is awakened multiple times, in fact, this is only because the computer always executes the program in order, and the simulation of this effect should already be deemed to be in line with the meaning of the question.


If debugging occurs, there will be a small problem, that is, there are many mice, and each mouse's run will have a wake man, so the host is awakened multiple times, in fact, this is only because the computer always executes the program in order, and the simulation of this effect should already be deemed to be in line with the meaning of the question.

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.