一道貓和老鼠吵醒主人的筆試題(C#)

CSDN帖子:http://community.csdn.net/Expert/topic/3839/3839240.xml?temp=.607937

7.程序設計:貓大叫一聲,所有的老鼠都開始逃跑,主人被驚醒。(C#語言)
要求:

    1.要有聯動性,老鼠和主人的行為是被動的。
    2.考慮可擴展性,貓的叫聲可能引起其他聯動效應

     大部分答案都是使用的事件編程,我這裏換了一下思路,使用觀察着模式,用接口也實現了,因爲考慮到第二個要求,即貓大叫也可能直接導致主人驚醒,所以Man也繼承了ICatCatcher接口

源代碼如下:

using System;

using System.Collections;

 

namespace test

{

 

     public interface ICatCatcher

     {

         void DoSth();

     }

 

     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)

         {

              catcherList.Add( catcher );

         }

 

         public void Miao()

         {

              Console.WriteLine( "Miao" );

 

              for(int i=0;i<catcherList.Count;i++)

              {

                   ICatCatcher catCatcher = (ICatCatcher)catcherList[i];

                   catCatcher.DoSth();

              }

         }

 

         [STAThread]

         public static void Main()

         {

              Cat cat = new Cat();

             

              Rat[] rat = new Rat[10];

              for( int i=0;i<10;i++)

              {

                   rat[i] = 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 DoSth()

         {

              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[i];

                   ratCatcher.Wake();

              }

         }

     }

 

     public class Man:ICatCatcher,IRatCatcher

     {

         public Man(IRatSubject[] ratSub,ICatSubject catSub)

         {

              for( int i=0 ;i<ratSub.Length;i++)

              {

                   ratSub[i].RegesiterRatCatcher(this);

              }

              catSub.RegesiterCatCatcher(this);

         }

 

         public void DoSth()

         {

              Console.WriteLine( "Cat bring on Wake" );

         }

        

         public void Wake()

         {

              Console.WriteLine( "Rats bring on Wake" );

         }

     }

}

        這裏如果調試會出現一點點小問題,就是老鼠有很多,每個老鼠的Run都會Wake一下Man,所以感覺是主人被多次驚醒,其實這只是因爲計算機總是按照順序來執行程序的,能夠模擬到這種效果應該已經算符合題意了

        這裏如果調試會出現一點點小問題,就是老鼠有很多,每個老鼠的Run都會Wake一下Man,所以感覺是主人被多次驚醒,其實這只是因爲計算機總是按照順序來執行程序的,能夠模擬到這種效果應該已經算符合題意了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章