AS3 Strategy策略模式翻譯(Design Patterns in ActionScript)

由於工作的需要學習Actionscript設計模式,我這裏順便翻譯,翻譯不好,呵呵。

       Today, we’re going to talk about the design patterns in ActionScript.You may be familiar with those patterns if you’re familiar with OO(object oriented). But I won’t suppose you have much knowledge about it, all you need to know is just some basic OO principles, such as encapsulation, inheritance and polymorphism.This is the first topic of design patterns, and you’ll see all the 23 design patterns in the following days. Hope you will enjoy these topics.Let’s begin the first one now.Suppose you need to write some codes to mimic some birds, the first is eagle, and the other is penguin. Those two animals have two methods named migration() and fly(). You may realize you need a super class named bird, and two methods in it. Then eagle and penguin can inherit from the super class, and override the methods,‘cause those birds have different behaviors. It seems to be perfect.You quickly write down the code.

      譯文:今天我們將討論關於Actionscript的設計模式。如果你熟悉面向對象編程(OO),你將熟悉這些設計模式。但是,我現在假設你很多這方面的知識,所有你需要知道僅僅是面向對象編程原則,比如封裝,繼承,多態。這裏我們將討論設計模式的第一個主題。你將下面的時間中看到23種設計模式。我也衷心希望你喜歡這些主題。讓我們開始第一個主題現在,假設我們現在實現生成鳥類的代碼。第一種是老鷹,其次是企鵝。這兩種動物都有兩個函數遷徙migration() 和飛行 fly()。你將意識到你需要一個這兩種動物父類叫鳥(bird),該類包含以上兩種方法。如何老鷹和企鵝可以繼承從父類並重寫父類的函數以便使得它們有不同的行爲。這看起來很完美,於是你寫下如下代碼:

class bird {
     public function migration():void{
     }
    public function fly():void{
    }
}
class eagle extends bird {
        public override function migration():void{
          trace("Sorry, I don't migrate");
       }
      public override function fly():void{
      trace("flying");
     }
}
class penguin extends bird {
       public override function migration():void

      {
         trace("migrating...");
      }
     public override function fly():void{
          trace("Sorry, I can't fly...");
       }
}
     OK, it does what you want. But what would you do when you want to add a new kind of birds into your program. Suppose you need to add a duck into your program now, you can inherit from the super class bird and override the methods again. It seems that the super class doesn’t save your time, reuse your code, you need to write every methods of subclasses. How to fix this problem? I think you need this design pattern—Strategy. And here is the definition from the GoF book. “Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.” And what’re the algorithms here? The methods, migration() and fly(). Every bird has it’s own behavior about migration and fly, it migrates or not, it flies or not. Let me say it more clearly, we just need to implement two situations about migration and two about fly. We need to implement each situation on a single class. And we’d better to have two interfaces named flyable and migrateable, and the classes just need to implement those interfaces. The UML diagram is showing below.

     譯文:好了,這就是你想要的。但是,當你想要編寫一種新的鳥進入你的代碼你該怎麼做呢。假設你現在想增加鴨子進入你的程序。你可以從父類Bird繼承方法並覆蓋其函數。這似乎看起來父類並沒有節約你的時間和代碼複用。你需要重寫父類所有方法,那麼如何解決這個問題呢?我想你需要策略設計模式。以下是在GOF(四人幫)設計模式書中對策略模式的定義:聲明一系列的策略,封裝每個策率,然後設計它們爲通用的。策略模式使得算法不依賴於使用其的客戶。那麼什麼又是算法在這裏呢?方法migration()和fly()。每種鳥類在遷移和飛行方面有自己不同的行爲。我們需要在每個類中實現它們每種情況。所以我們最好需要兩個接口:flyable ,migrateable。這些類需要實現這兩個接口。以下就是其UML圖:

 

 


 

Now, the super class bird needs to handle two variables of those interfaces. Using polymorphism, the super class bird can do all things we do in the subclasses. When you want to ask a eagle to fly, you can simply call the function doFlying() of class bird. You can write the code like this:
var littleEagle:bird = new eagle();
littleEagle.doFlying();
I know you may be confused now, let me show you the code of the super class bird.

現在,Bird父類需要處理這兩個接口變量屬性。那就是使用OOP多態特性,即父類Bird可以實現其子類可以實現的行爲。當你想要實現鷹飛的行爲,你可以簡單調用鷹的Flying函數即可。你將寫如下代碼:

var littleEagle:bird = new eagle();
littleEagle.doFlying();

我知道你現在可能有些混淆迷茫了,讓我把父類Bird的代碼顯示出來看看吧:

class bird {
   var flyInterface:flyable;
   var migrateInterface:migrateable;
   public function bird(_fly:flyable,_migrate:migrateable)

  {
        flyInterface = _fly;
      migrateInterface = _migrate;
    }

    public function doMigration():void

  {
      migrateInterface.doMigration();
   }
   public function doFlying():void

  {
         flyInterface.flying();
   }
}

If you want to add a new kind of birds, all you need to do is just write a new class, and specify the constructor. And that’s it! If you want to change the behavior of fly or neverFly, you just need to change the code of the specific class. And all the birds’ behavior will get changed. So, the code will be more easily to maintain. In this sample the behavior is easy to change, so we need to encapsulate the behavior. The UML of all classes is showing below.

 

當你需要增加一種新的鳥類,你所需要做的就是寫一個新的類(繼承父類Bird)並在實現其構造函數。這就是你所需要做的。如果你想要改變飛(fly)和不飛(neverFly)的行爲,你僅僅需要改變這個子類的代碼即可。這樣這種鳥的飛行行爲就會改變了。這樣的代碼就會變更容易維護了。在這個例子鳥的行爲是很容易改變的,所以我們需要封裝其行爲。以下就是所有UML圖顯示:

 



 
      Let’s back to the definition of this pattern, “Define a family of algorithms, encapsulate each one”, we have done it. And the result is we can change the behavior or add a new bird more easily. The algorithm means the way you solve a problem, when you have many ways to solve a problem, encapsulate it, and make it interchangeable. This sample is not good enough to show the power of this pattern, if you want to learn more about this pattern, you can look up the GoF book.

     現在讓我們回頭看看這個設計模式的定義吧:聲明一系列的策略,封裝每個策率,然後設計它們爲通用的。到目前我們已經完成。這個使得我們更容易改變鳥的行爲或增加一種新的鳥。這個算法就是告訴你解決問題的方式。當你有很多方式去解決一個問題的時候,封裝它並設計其爲通用的。這個例子的代碼並不很好顯示這個設計模式的強大。如果你需要了解更多的關於這個模式的知識請查閱斯坦人幫的設計模式書記

發佈了17 篇原創文章 · 獲贊 0 · 訪問量 2944
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章