The Strategy Pattern - Changing Behavior

Monday, December 1st, 2008 | examples

The Strategy Pattern is used to change behavior. In this example, you change the behavior the weapon by swapping in and out weapons (and the projectiles they fire).

When looking through the code, focus on the weapons (Gun, MissleLauncher, and DartBlower) and the “fire()” function in “ShooterBasicView.as”. You’ll notice that ShooterBasicView only cares that the weapon is an “IFireable and then it’s just going to fire it.

The weapons are swapped in the document class, “ShooterMain” in the Keyboard handler.


source

*If you’d like to challenge yourself, try adding a fourth weapon that uses Planes as ammo.

Tags:

3 Comments to The Strategy Pattern - Changing Behavior

baixiaozhe
December 2, 2008

clike which key to fire?

baixiaozhe
December 2, 2008

Keyboard.SPACE!

P48L0
December 4, 2008

Great job spreading the design patterns and the benefits of it use on real life situations!

Nice example!

Leave a comment