Design Patterns in ActionScript-Visitor

crab-128x128 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.

Var team : Array = new Array();

team.push(new DwarvenSniper());

team.push(new MountainKing());
team.push(new Priest());

function attack(team:Array):void
{
for(var i:int = 0; i < team.length; i++)
{

If (team[i] instanceof DwarvenSniper)

DwarvenSniper(Team[i]).gunShot();

Else if(team[i] instanceof MountainKing)

MountainKing(Team[i]).hammerShot();

Else if(team[i] instanceof Priest)

Priest(Team[i]).priestHit();

}
}

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.

Function creatureAttack(team:Array):void

{

If(………)

//Using it’s skill here

Else if(…………)

.

.

.

.

Else if(………..)

//Using it’s own skill here

}

Function buildingAttack(team:Array):void

{

…..//the same if-else clauses with different skills

}

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.

Var creatureAttack: AttackVisitor = new creatureAttack();

For(var i:int = 0; I < team.length; i++)
(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.

Function accept(visitor:AttackVisitor):void
{
Visitor.visitElementX(this);
}

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

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

3 Responses to “Design Patterns in ActionScript-Visitor”

  1. I lost where you where going with the last example. Can you illustrate more what the first example in the article would look like once implemented with this pattern?

  2. My main question is within the ConcreteVisitor1 on the diagram- would would visitElementX do exactly? In this example would be something like this:

    public function visitElementPriest(priest:Priest) {
    priest.priestHit();
    }

    What if priestHit() looked like this priestHit(target:Enemy)? How would target be integrated with this pattern… maybe as a constructor param of creatureAttack?

  3. Liu Bo says:

    to Jonathan,
    you’re right:), You can put the target as a constructor parameter. Actually, I haven’t considered the problem of how to hit the target, I just want to show the refactoring of if-else statements here.
    It’s my fault, and thanks for your comment :)

Leave a Reply