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. DownloadDownload Full Project

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值