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

Design Patterns in ActionScript-Visitor

阅读更多

It’s our winter holiday now, and I spend many times in playing games. In last week, I soaked myself in <Prince of Persia: the sands of time>. I love this game, but it’s a pity for me that when I fight to the monsters, I can’t control the heroine. Because of that sometimes I need her help to shot somebody exactly not the other one. OK, I’m crazy :) Controlling two roles will increase the difficulty of manipulation, and it’s not a RTS game.

 

In RTS game, such as Warcraft, you can control many units. When your team attacks someone, the team members will use their own skills. Maybe the Dwarven Sniper will use his gun to shot, while the Mountain King uses his hammer. So, in action script, we may express these in this way.

  1. Var team : Array = new Array () ;
  2.  
  3. team . push ( new   DwarvenSniper ()) ;
  4.  
  5. team . push ( new   MountainKing ()) ;
  6. team . push ( new   Priest ()) ;
  7.  
  8. function   attack ( team : Array ) : void
  9. {
  10. for ( var   i : int = 0 ; i < team . length ; i ++ )
  11. {
  12.  
  13. If   ( team [ i ] instanceof DwarvenSniper )
  14.  
  15. DwarvenSniper ( Team [ i ]) . gunShot () ;
  16.  
  17. Else   if ( team [ i ] instanceof MountainKing )
  18.  
  19. MountainKing ( Team [ i ]) . hammerShot () ;
  20.  
  21. Else   if ( team [ i ] instanceof Priest )
  22.  
  23. Priest ( Team [ i ]) . priestHit () ;
  24.  
  25. }
  26. }

Note: the above code is directly type in Word, so don’t try to complier it, it may contain many grammar mistakes :)

Now, take a look at the attack function. When you pass the team array into it, it may works well. Actually, the team member maybe more than ten, eh, I mean the type of members. So, we need to distinguish them in the iteration. This will cause many if-else. And this is the “bad smell” of our code :)

Here, our problem is that, in an array that may contains many objects, and the action it takes depends on the object type. Further more, the object type is fixed, and the operations of each type are also known, just as the units in Warcraft. What we want to do is organize their basic operations to form a series operation, such as a team in Warcraft to attack the creatures with their normal skills, or attack the buildings with siege skills.

  1. Function creatureAttack ( team : Array ) : void
  2.  
  3. {
  4.  
  5. If ( ……… )
  6.  
  7. //Using it’s skill here
  8.  
  9. Else   if ( ………… )
  10.  
  11. .
  12.  
  13. .
  14.  
  15. .
  16.  
  17. .
  18.  
  19. Else   if ( ……….. )
  20.  
  21. //Using it’s own skill here
  22.  
  23. }
  24.  
  25. Function   buildingAttack ( team : Array ) : void
  26.  
  27. {
  28.  
  29. ….. //the same if-else clauses with different skills
  30.  
  31. }

Actually, our aim is to refactoring the if-else clauses. And that’s what the Visitor pattern does. The intent is as follows.

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

–By GOF BOOK

And here is the static diagram of this pattern from the GoF book.

clip_image001

In the Warcraft example, the member is corresponding to the Element, eh, one for each concrete element. And the array is the ObjectStructure. The attacks is the visitor, the concrete visitor is the creature attack and building attack.

So, we can refactor the code by this pattern, let all the members implements the Element interface, and let the attacks implements the Visitor interface. And in the iteration, we can do it in this way.

  1. Var creatureAttack : AttackVisitor = new creatureAttack () ;
  2.  
  3. For ( var   i : int = 0 ; I < team . length ; i ++ )
  4. ( teamElement ) team [ i ] . accept ( creatureAttack ) ;

Here, one accept method replace all the if-else clauses and the concrete operations. Eh, you should implement every accept method in the concrete element like this.

  1. Function accept ( visitor : AttackVisitor ) : void
  2. {
  3. Visitor . visitElementX ( this ) ;
  4. }

If you’re interested in this pattern, find more information by Google :) And you can take a look at the example code in the attach file. Download Download Full Project

分享到:
评论

相关推荐

    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开发基础的开发者,通过掌握设计模式的实践应用,提升软件开发的生产...

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

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

    java-design-patterns-master_JAVAdesignpattens_java_design_

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

Global site tag (gtag.js) - Google Analytics