Wednesday, May 20, 2009

Model a fighter with STRATEGY design pattern

Below you will find classes and interfaces for a martial artist. You will find classes for individual fighters along with classes for movements the fighters can take during a combat. Each fighter can make only one movement at a time, but can change movement at any time during a combat.

First All, Fighter is an abstract concept. He/She can be Gracie Jiu-Jitsu, Capoeira, American Kenpo, Jeet Kune Do, Shaolin fighters... or mixed style fighter. He/She may have unique looking, nationality, age etc. During combat, fighters have something in common, their movement can be classified into attack and defense.

There are many attack strategies such as punch, tick, elbow, knees, joint knock; as many defense strategies as well -- block, trap, lock, duck, roll, footsweep... The problem is, you never know how many strategies a fighter has learned, and a fighter could invent a new strategy today!

The strategy design pattern resolve this by defining a family of movements, encapsulates each one, and make them interchangable by extending the same interfaces (attack or defense), as a result, new movement classes can be added without modifying other parts of the system.


Show   Hide

No comments:

Post a Comment

meta.ai impression

Meta.ai is released by meta yesterday, it is super fast you can generate image while typing! You can ask meta.ai to draw a cat with curvy fu...