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:

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

    Nice example!
  • baixiaozhe
    Keyboard.SPACE!
  • baixiaozhe
    clike which key to fire?
blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

December 2008
M T W T F S S
« Nov   Jan »
1234567
891011121314
15161718192021
22232425262728
293031  

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...