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
/*Fighter.java*/ public abstract class Fighter { AttackMovement attackmove; DefenseMovement defensemove; public Fighter() {} public abstract void display(); public void performAttack(){ attackmove.attack(); } public void performDefense() { defensemove.defense(); } public void speak() { System.out.println(" Fighters fight. "); } public void setAttack(AttackMovement a) { this.attackmove = a; } public void setDefense(DefenseMovement d) { this.defensemove = d; } } |
/*AttackMovement.java*/ public interface AttackMovement { public void attack(); } |
/*Punch.java*/ public class Punch implements AttackMovement{ public void attack() { System.out.println("I'm punching"); } } |
/*Tick.java*/ public class Tick implements AttackMovement{ public void attack() { System.out.println("I'm ticking"); } } |
/*DefenseMovement.java*/ public interface DefenseMovement { public void defense(); } |
/*Block.java*/ public class Block implements DefenseMovement{ public void defense(){ System.out.println("I'm blocking"); } } |
/*Duck.java*/ public class Duck implements DefenseMovement{ public void defense(){ System.out.println("I'm ducking"); } } |
/*ShaoLinFighter.java*/ public class ShaoLinFighter extends Fighter{ public ShaoLinFighter() { this.attackmove = new Punch(); this.defensemove = new Block(); } public void display() { System.out.println("I look like a ShaoLin monk."); } public void speak() { System.out.println("Daily practise the apparently simple exercise of breathing in and out slowly and deeply."); } } |
/*JiuJitSuFighter.java*/ public class JiuJitSuFighter extends Fighter{ public JiuJitSuFighter() { this.attackmove = new Tick(); this.defensemove = new Block(); } public void display() { System.out.println("I Wear Judo uniform with black belt."); } public void speak() { System.out.println("I am a shark. The ground is my ocean."); } } |
/*ArenaSimulator.java*/ public class ArenaSimulator { public static void main(String[] args) { Fighter fighter1 = new ShaoLinFighter(); Fighter fighter2 = new JiuJitSuFighter(); fighter1.display(); fighter1.speak(); System.out.println(""); fighter2.display(); fighter2.speak(); fighter1.performAttack(); fighter2.performDefense(); fighter1.setAttack(new Tick()); fighter2.setDefense(new Duck()); fighter1.performAttack(); fighter2.performDefense(); } } |
No comments:
Post a Comment