`
shake863
  • 浏览: 661487 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Design Patterns in ActionScript-Strategy

阅读更多

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.

Search-256x256 Demo | Download Download Full Project

 

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.

  1. class bird {
  2. public   function migration () : void {
  3. }
  4.  
  5. public   function fly () : void {
  6. }
  7. }
  8.  
  9. class   eagle extends bird {
  10. public   override function migration () : void {
  11. trace ( " Sorry, I don’t migrate " ) ;
  12. }
  13.  
  14. public   override function fly () : void {
  15. trace ( " flying " ) ;
  16. }
  17. }
  18.  
  19. class   penguin extends bird {
  20. public   override function migration () : void {
  21. trace ( " migrating… " ) ;
  22. }
  23.  
  24. public   override function fly () : void {
  25. trace ( " Sorry, I can’t fly… " ) ;
  26. }
  27. }

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.

clip_image002

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:

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

I know you may be confused now, let me show you the code of the super class bird.

  1. class bird {
  2. var   flyInterface : flyable ;
  3. var   migrateInterface : migrateable ;
  4.  
  5. public   function bird ( _fly : flyable , _migrate : migrateable ){
  6. flyInterface = _fly ;
  7. migrateInterface = _migrate ;
  8. }
  9.  
  10. public   function doMigration () : void {
  11. migrateInterface . doMigration () ;
  12. }
  13.  
  14. public   function doFlying () : void {
  15. flyInterface . flying () ;
  16. }
  17. }

From the code, we can see the super class do all the things simply by call the methods of the interfaces. We don’t have to know which instance the interface points to, we only need to know it works. That’s it. Well, we’ll use the constructor to specify the instances of each interface. And here comes the code of each subclass.

  1. class eagle extends bird {
  2. public   function eagle (){
  3. super ( new   fly () , new neverMigration ()) ;
  4. }
  5.  
  6. }
  7.  
  8. class   penguin extends bird {
  9.  
  10. public   function penguin (){
  11. super ( new   neverFly () , new migration ()) ;
  12. }
  13.  
  14. }

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.

clip_image004

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.

分享到:
评论

相关推荐

    Go Design Patterns for Real-World Projects epub

    Go Design Patterns for Real-World Projects 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Go Design Patterns for Real-World Projects azw3

    Go Design Patterns for Real-World Projects 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Data Structures and Algorithms with Object-Oriented Design Patterns in CSharp - Bruno R. Preiss.chm

    Data Structures and Algorithms with Object-Oriented Design Patterns in CSharp - Bruno R. Preiss

    Laracasts - design-patterns-in-php

    "Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...

    Design-Patterns-in-Modern-C++

    The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...

    design-patterns-for-humans设计模式中文翻译版

    https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码

    Design Patterns in Modern C++--2018

    Apply design patterns to modern C++ programming Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and ...

    Design-Patterns-In-Kotlin,在kotlin中实现的设计模式.zip

    《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。

    Design Patterns in ActionScript

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

    Go Design Patterns for Real-World Projects.epub

    Go: Design Patterns for Real-World Projects What You Will Learn: Install and configure the Go development environment to quickly get started with your first program Use the basic elements of the ...

    Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3

    Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3

    Pro-Objective-C-Design-Patterns-for-iOS

    《Pro-Objective-C-Design-Patterns-for-iOS》是一本专注于在iOS平台上利用Objective-C语言实现设计模式的专业书籍。书中旨在帮助已经有一定Cocoa开发基础的开发者,通过掌握设计模式的实践应用,提升软件开发的生产...

    java-design-patterns-master_JAVAdesignpattens_java_design_

    这个压缩包“java-design-patterns-master”显然是一个专注于Java设计模式的学习资源,旨在帮助开发者深入理解和应用这些模式。下面我们将详细探讨Java设计模式及其在实际开发中的应用。 1. **单例模式(Singleton...

    design-patterns-c-sharp

    "design-patterns-c-sharp"这个主题涵盖了C#编程环境中如何应用这些模式的知识。 首先,我们需要理解设计模式的基本概念。设计模式并不具体到任何特定的代码或库,而是一种描述在特定情况下如何设计软件组件的通用...

    2010-13-Design Patterns for Safety-Critical.pdf

    在本论文“2010-13-Design Patterns for Safety-Critical”中,作者Ashraf Armoush探讨了如何将设计模式的概念应用于安全关键嵌入式系统的开发。他构建了一个设计模式目录,这个目录包含了一系列针对硬件和软件的...

Global site tag (gtag.js) - Google Analytics