乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      設(shè)計(jì)模式學(xué)習(xí)筆記(六)——Prototype原型模式

       skywood 2007-08-06

             Prototype原 型模式是一種創(chuàng)建型設(shè)計(jì)模式,它主要面對(duì)的問題是:“某些結(jié)構(gòu)復(fù)雜的對(duì)象”的創(chuàng)建工作;由于需求的變化,這些對(duì)象經(jīng)常面臨著劇烈的變化,但是他們卻擁有比 較穩(wěn)定一致的接口。感覺好像和前幾篇所說的設(shè)計(jì)模式有點(diǎn)分不清,下面我們先來回顧一下以前的幾種設(shè)計(jì)模式,予以區(qū)分,再來說說原型模式。

             Singleton單件模式解決的問題是:實(shí)體對(duì)象個(gè)數(shù)問題(這個(gè)現(xiàn)在還不太容易混)

             AbstractFactory抽象工廠模式解決的問題是:“一系列互相依賴的對(duì)象”的創(chuàng)建工作

             Builder生成器模式解決的問題是:“一些復(fù)雜對(duì)象”的創(chuàng)建工作,子對(duì)象變化較頻繁,對(duì)算法相對(duì)穩(wěn)定

            FactoryMethor工廠方法模式解決的問題是:某個(gè)對(duì)象的創(chuàng)建工作

            再回來看看今天的Prototype原型模式,它是用來解決“某些結(jié)構(gòu)復(fù)雜的對(duì)象”的創(chuàng)建工作。現(xiàn)在看看,好象還是差不多。這個(gè)問題先放在這,我們先往下看Prototype原型模式。

            《設(shè)計(jì)模式》中說道:使用原型實(shí)例指定創(chuàng)建對(duì)象的種類,然后通過拷貝這些原型來創(chuàng)建新的對(duì)象。

            此時(shí)注意:原型模式是通過拷貝自身來創(chuàng)建新的對(duì)象,這一點(diǎn)和其他創(chuàng)建型模式不相同。好,我們?cè)賮砜纯丛湍J降慕Y(jié)構(gòu)


            
      這個(gè)結(jié)構(gòu)說明原型模式的客戶端程序(ClientApp)是依賴于抽象(Prototype),而對(duì)象的具體實(shí)現(xiàn)也是依賴于抽象(Prototype)。符合設(shè)計(jì)模式原則中的依賴倒置原則——抽象不應(yīng)依賴于具體實(shí)現(xiàn),具體實(shí)現(xiàn)應(yīng)依賴于抽象。

             我們現(xiàn)在回來看看原型模式的實(shí)現(xiàn),我定義了一個(gè)場(chǎng)景,一個(gè)人開這一輛車在一條公路上?,F(xiàn)在這件事是確定的,但不確定的有幾點(diǎn):1、人:姓名,性別,年齡;2車:什么牌子的;3公路:公路名字,長(zhǎng)度,類型(柏油還是土路)。現(xiàn)在我們一個(gè)個(gè)實(shí)現(xiàn)。

             先來實(shí)現(xiàn)人,定義一個(gè)抽象類,AbstractDriver,具體實(shí)現(xiàn)男性(Man)和女性(Women

      public abstract class AbstractDriver

          {

              public AbstractDriver()

              {

                  //

                  // TODO: 在此處添加構(gòu)造函數(shù)邏輯

                  //

              }

       

              public string name;

              public string sex;

              public int age;

       

              public abstract string drive();

       

              public abstract AbstractDriver Clone();

          }

       

          public class Man:AbstractDriver

          {

              public Man(string strName,int intAge)

              {

                  sex = "Male";

                  name = strName;

                  age = intAge;

              }

       

              public override string drive()

              {

                  return name + " is drive";

              }

       

              public override AbstractDriver Clone()

              {

                  return (AbstractDriver)this.MemberwiseClone();

              }

          }

       

          public class Women:AbstractDriver

          {

              public Women(string strName,int intAge)

              {

                  sex = "Female";

                  name = strName;

                  age = intAge;

              }

       

              public override string drive()

              {

                  return name + " is drive";

              }

       

              public override AbstractDriver Clone()

              {

                  return (AbstractDriver)this.MemberwiseClone();

              }

          }

          注意:抽象代碼中有一個(gè)Clone的方法,個(gè)人認(rèn)為這個(gè)方法是原型模式的一個(gè)基礎(chǔ),因?yàn)榍懊嬷v了原型模式是通過拷貝自身來創(chuàng)建新的對(duì)象。

            下面我們?cè)賮韺?shí)現(xiàn)公路和汽車

            公路:

      public abstract class AbstractRoad

          {

              public AbstractRoad()

              {

                  //

                  // TODO: 在此處添加構(gòu)造函數(shù)邏輯

                  //

              }

       

              public string Type;

              public string RoadName;

              public int RoadLong;

       

              public abstract AbstractRoad Clone();

          }

       

          public class Bituminous:AbstractRoad    //柏油路

          {

              public Bituminous(string strName,int intLong)

              {

                  RoadName = strName;

                  RoadLong = intLong;

                  Type = "Bituminous";

              }

       

              public override AbstractRoad Clone()

              {

                  return (AbstractRoad)this.MemberwiseClone();

              }

          }

       

          public class Cement:AbstractRoad        //水泥路

          {

              public Cement(string strName,int intLong)

              {

                  RoadName = strName;

                  RoadLong = intLong;

                  Type = "Cement";

              }

       

              public override AbstractRoad Clone()

              {

                  return (AbstractRoad)this.MemberwiseClone();

              }

          }

         

          汽車:

          public abstract class AbstractCar

          {

              public AbstractCar()

              {

                  //

                  // TODO: 在此處添加構(gòu)造函數(shù)邏輯

                  //

              }

       

              public string OilBox;

              public string Wheel;

              public string Body;

       

              public abstract string Run();

              public abstract string Stop();

       

              public abstract AbstractCar Clone();

          }

       

          public class BMWCar:AbstractCar

          {

              public BMWCar()

              {

                  OilBox = "BMW‘s OilBox";

                  Wheel = "BMW‘s Wheel";

                  Body = "BMW‘s body";

              }

       

              public override string Run()

              {

                  return "BMW is running";

              }

       

              public override string Stop()

              {

                  return "BMW is stoped";

              }

       

              public override AbstractCar Clone()

              {

                  return (AbstractCar)this.MemberwiseClone();

              }

          }

       

          public class BORACar:AbstractCar

          {

              public BORACar()

              {

                  OilBox = "BORA‘s OilBox";

                  Wheel = "BORA‘s Wheel";

                  Body = "BORA‘s Body";

              }

       

              public override string Run()

              {

                  return "BORA is running";

              }

       

              public override string Stop()

              {

                  return "BORA is stoped";

              }

       

              public override AbstractCar Clone()

              {

                  return (AbstractCar)this.MemberwiseClone();

              }

          }

       

          public class VolvoCar:AbstractCar

          {

              public VolvoCar()

              {

                  OilBox = "Volvo‘s OilBox";

                  Wheel = "Volvo‘s Wheel";

                  Body = "Volvo‘s Body";

              }

       

              public override string Run()

              {

                  return "Volvo is running";

              }

       

              public override string Stop()

              {

                  return "Volvo is stoped";

              }

       

              public override AbstractCar Clone()

              {

                  return (AbstractCar)this.MemberwiseClone();

              }

          }

          然后我們?cè)賮砜纯磮?chǎng)景,我們定義一個(gè)Manage類,在這個(gè)場(chǎng)景中有一個(gè)人,一輛車和一條公路,代碼實(shí)現(xiàn)如下:

      class Manage

          {

              public AbstractCar Car;

              public AbstractDriver Driver;

              public AbstractRoad Road;

       

              public void Run(AbstractCar car,AbstractDriver driver,AbstractRoad road)

              {

                  Car = car.Clone();

                  Driver = driver.Clone();

                  Road = road.Clone();

              }

          }

          可以看到,在這個(gè)代碼中,場(chǎng)景只是依賴于那幾個(gè)抽象的類來實(shí)現(xiàn)的。最后我們?cè)賮韺?shí)現(xiàn)一下客戶代碼,比如我現(xiàn)在要一輛Volvo車,一個(gè)叫“Anli”的女司機(jī),在一條叫“Road1、長(zhǎng)1000的柏油路上。

              static void Main(string[] args)

              {

                  Manage game = new Manage();

                  game.Run(new VolvoCar(),new Women("Anli",18),new Bituminous("Road1",1000));

                  Console.Write("CarRun:" + game.Car.Run() + "\n");

                  Console.Write("DriverName:" + game.Driver.name + "\n");

                  Console.Write("DriverSex:" + game.Driver.sex + "\n");

                  Console.Write("RoadName:" + game.Road.RoadName + "\n");

                  Console.Write("RoadType:" + game.Road.Type + "\n");

                  Console.Write("CarStop:" + game.Car.Stop() + "\n");

                  Console.Read();

              }

          運(yùn)行的結(jié)果是:

          CarRun:Volvo is running

      DriverName:Anli

      DriverSex:Female

      RoadName:Road1

      RoadType:Bituminous

      CarStop:Volvo is stoped

       

      如果我現(xiàn)在想換成BORA車,讓我(kid-li)開,在一個(gè)水泥馬路上,我們只要更改Main函數(shù)中Run的實(shí)參。

      game.Run(new BORACar(),new Man("kid-li",24),new Cement("Road1",1000));

      運(yùn)行結(jié)果是:

      CarRun:BORA is running

      DriverName:kid-li

      DriverSex:Male

      RoadName:Road1

      RoadType:Cement

      CarStop:BORA is stoped

      這樣,經(jīng)過簡(jiǎn)單的更改,可以實(shí)現(xiàn)實(shí)現(xiàn)細(xì)節(jié)的變化。

      現(xiàn)在我們?cè)賮砜纯丛湍J降膸讉€(gè)要點(diǎn):

      1Prototype模式同樣用于隔離類對(duì)象的使用者和具體類型(易變類)之間的耦合關(guān)系,它同樣要求這些“易變類”擁有“穩(wěn)定的接口”。

      2、Prototype模式對(duì)于“如何創(chuàng)建易變類的實(shí)體對(duì)象”采用“原型克隆”的方法來實(shí)現(xiàn),它使得我們可以非常靈活地動(dòng)態(tài)創(chuàng)建“擁有某些穩(wěn)定接口”的新對(duì)象——所需工作僅僅是注冊(cè)一個(gè)新類的對(duì)象(即原型),然后在任何需要的地方不斷地Clone。

      3、Prototype模式中的Clone方法可以利用Object類的MemberwiseClone()或者序列化來實(shí)現(xiàn)深拷貝。

      這里面我們?cè)賮碚f說淺拷貝和深拷貝。我想對(duì)于Prototype模式是很重要的。我覺得淺拷貝和深拷貝的關(guān)鍵區(qū)別是對(duì)于引用對(duì)象的拷貝。例如我們有一個(gè)類Class1,

      public class Class1

      {

          int a;

          int[] b;

      }

      我們用淺拷貝實(shí)現(xiàn)了兩個(gè)對(duì)象c1c2,對(duì)于c1.ac2.a,他們所有的內(nèi)存空間是不一樣的,但是c1.bc2.b,由于它們是引用類型,在淺拷貝時(shí)只是拷貝了一個(gè)地址給b成員,實(shí)際上c1.bc2.b指向同一塊內(nèi)存。

      但如果我們用深拷貝,c1.bc2.b指向的是不同的內(nèi)存地址。那又如何實(shí)現(xiàn)深拷貝呢?我們可以利用序列化和反序列化來實(shí)現(xiàn)。

      對(duì)于淺拷貝和深考貝的問題,我的同事TerryLee曾寫過一篇文章《小議.NET中的對(duì)象拷貝》,我在這就不贅述了。

      posted on 2006-05-18 15:54 KiddLee 閱讀(1415) 評(píng)論(2)  編輯 收藏 引用 網(wǎng)摘 所屬分類: 設(shè)計(jì)模式

      FeedBack:
      # re: 設(shè)計(jì)模式學(xué)習(xí)筆記(六)——Prototype原型模式 2006-07-18 11:02 大雁
      我 這里實(shí)現(xiàn)不了MemberwiseClone方法,我就用了clone方法代替,然后我調(diào)用了Manage.Car和Manage.Driver, Manage.Road中的方法,編譯出錯(cuò),提示: java.lang.NullPointerException,不解???既然manage.run方法里已經(jīng)實(shí)現(xiàn)了clone方法,為什么還會(huì)這樣?   回復(fù)  更多評(píng)論

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多