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