`
suiyuan0808
  • 浏览: 155208 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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.

     现在让我们回头看看这个设计模式的定义吧:声明一系列的策略,封装每个策率,然后设计它们为通用的。到目前我们已经完成。这个使得我们更容易改变鸟的行为或增加一种新的鸟。这个算法就是告诉你解决问题的方式。当你有很多方式去解决一个问题的时候,封装它并设计其为通用的。这个例子的代码并不很好显示这个设计模式的强大。如果你需要了解更多的关于这个模式的知识请查阅斯坦人帮的设计模式书记

  • 大小: 19.6 KB
  • 大小: 31 KB
分享到:
评论

相关推荐

    Design Patterns in ActionScript

    《ActionScript设计模式》是软件开发领域中针对ActionScript编程语言的一种实践指南,它深入探讨了如何在ActionScript项目中应用经典的设计模式。设计模式是软件工程中的宝贵经验总结,它们是解决常见问题的可复用...

    Design Patterns in C#

    design pattern in C# language

    Design Patterns in Modern C++

    Design Patterns in Modern C++: Reusable Approaches for Object-Oriented Software Design English | PDF| 2018 | 312 Pages | ISBN : 1484236025Design Patterns in Modern C++: Reusable Approaches for Object...

    Design Patterns for Embedded Systems in C

    通过学习《Design Patterns for Embedded Systems in C》,读者能够了解如何在C语言的约束下运用设计模式来解决嵌入式系统开发中遇到的问题,提高嵌入式软件的开发效率和系统性能,减少错误,缩短开发周期。

    Design Patterns in PHP and Laravel

    Too often design patterns are explained using tricky concepts, when in fact they are easy to use and can enrich your everyday development. Design Patterns in ...

    Introduction to Design Patterns in C Wit Alan Ezust

    Alan Ezust在其著作《An Introduction to Design Patterns in C++ with Qt™, 2nd Edition》中探讨了设计模式的这一概念,并且在Qt框架的基础上讲解了如何在C++项目中应用这些设计模式。 C++是一种高性能、多范式的...

    《Java Design Patterns》高清完整PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    Design Patterns by Tutorials Learning design patterns in Swift 4.2, 2nd Edition

    本书《Design Patterns by Tutorials Learning design patterns in Swift 4.2, 2nd Edition》主要介绍在Swift 4.2环境下,如何通过教程学习设计模式。设计模式是软件开发中的一个重要概念,它是一套被反复使用、多数...

    《设计模式》中文版 Design Patterns

    设计模式 中文版 Design Patterns 可复用面向对象软件基础 经典之作 内含23个设计模式

    Design Patterns in Java

    本资料《Design Patterns Java Companion》由James W. Cooper撰写于1998年10月2日,版权归属于作者本人。书中全面地介绍了23种常用的设计模式,并通过Java底层类进行深入解析。 #### 设计模式定义及其来源 设计...

    App Architecture: iOS Application Design Patterns in Swift 最新中文版含源码

    App Architecture: iOS Application Design Patterns in Swift 包含Source code 有钱请支持正版 没钱请默默学习 原书地址: https://www.objc.io/books/app-architecture 中文原书地址: ...

    GO语言设计模式 go-design-patterns

    The book starts with a brief introduction to Go programming essentials and quickly moves on to explain the idea behind the creation of design patterns and how they appeared in the 90's as a common ...

    Data Structures And Algorithms With Object-oriented Design Patterns In Java

    Data Structures And Algorithms With Object-oriented Design Patterns In Java.chm

Global site tag (gtag.js) - Google Analytics