Tuesday, June 26, 2012

Suppose you have created a game 

There a role is a swordsman 
class SwordsMan{ private String name; public SwordsMan(String userName){ name = userName; } public void run(){ System.out.println(name + " "); } public void fight(){ System.out.println(name + " "); } } 



Which have a role is a magician 
 class Magician { private String name; public Magician(String userName){ name = userName; } public void run(){ System.out.println(name + " "); } public void fight(){ System.out.println(name + " "); } } 


At this time you will think of each role, which has a repeat of the run method is so good, I wrote a method and then let all the role of inheritance (inheritance) 
So he wrote this program 
 class Character{ protected String name; public Character(String userName){ name = userName; } protected void run(){ System.out.println(name +" 跑走"); } public void fight(){} } 


Attack because each role is different then all roles are to overthrow the fight () method (override) 
 class SwordsMan extends Character{ public SwordsMan(String username){ super(username); } public void fight(){ System.out.println(name + " "); } } class Magician extends Character{ public Magician(String username){ super(username); } public void fight(){ System.out.println(name + " "); } } 


... Other role is the same inheritance to override ... 

At this time magician, swordsman, or other roles instantiation (instance) 
You can use their own methods 

Nevertheless, such a problem will emerge, that is, the program flexible enough 
Want to see that I have 50 roles, there are 20 roles attacks are waving the sword 
So I have to go to each role override fight () is not exhausted. 

Besides, if I want to modify the role of each one brandishing a sword into a brandished knife, 
This approach is also waste a lot of time, 
So we have to put often changes in part taken out, 

Therefore, we will fight () out interface (interface) to be written 
 interface FightMethod{ public void fight(String name); } 


We then use this category of SwordsMan to inherit it? 
No! 

We have inherited the Character, Java does not allow multiple inheritance, so we implement (implement) it? 
No! This fight with just overwrite () method is not on the same issue? 
Compound is the answer we want! 

We define a type of sword to implement it 
 class UseSword implements FightMethod{ public void fight(String name){ System.out.println(name + " "); } } 

Then changed so that the Swordsman class
 class SwordsMan extends Character{ private UseSword charUseSword; public SwordsMan(String username){ super(username); charUseSword = new UseSword(); charUseSword.fight(username); } } 



Like this very problem! Problem?

Recall just happened here, so that each role must declare an attack object,
And entities to it, the 100 roles still need to configure the 100 entities,
You will be exhausted ...

How should we do?

Polymorphism is in this place such a declaration
 FightMethod fm = new UseSword(); fm.fight(); FightMethod fm是形式型態的宣告,而new UseSword()才是你實際型態的宣告。 


Polymorphism (Polymorphism) when the actual type of a variable (actual type) and form type
(Formal type) is inconsistent with the method call this variable will call the "correct" version.
That is, the version of the actual type.

Therefore, we can thus directly in the Character class so declared
 class Character{ public String name; protected FightMethod fm; public Character(String userName){ name = userName; } public void run(){ System.out.println(name +" "); } protected void useWeapon(){ fm.fight(name); } } 

In SwordsMan within such declared
 class SwordsMan extends Character{ public SwordsMan(String username){ super(username); fm = new UseSword(); } } 


So the swordsman want to change the attack can be directly replaced by useKnife ();
Or to an increase of one hundred roles, can also increase the attack on the interface.

Complete code
  class Character {
     protected String name;
 protected FightMethod fm;
 public Character (String userName) {
 name = userName;
 }
 public void run () {
 System.out.println (name + "run away");
 }
 protected void useWeapon () {
 fm.fight (name);
 }
 }
 interface FightMethod {
 public void fight (String name);
 }
 class UseSword implements FightMethod {
 public void fight (String name) {
 System.out.println (name + "waving the sword");
 }
 }
 class UseMagic implements FightMethod {
     public void fight (String name) {
         System.out.println (name + "magic");
     }
 }
 class SwordsMan extends Character {
 public SwordsMan (String username) {
 super (username);
 fm = new UseSword ();
 }

 }
 class Magician extends Character {
 public Magician (String username) {
 super (username);
 fm = new UseMagic ();
 }

 }
 class TestJava { 
     public static void main (String [] args) {
         SwordsMan swordMan = new SwordsMan ("John");
         Magician magician = new Magician ("Mary");
         swordMan.run ();
         magician.useWeapon ();
         swordMan.useWeapon ();
     }
 }

Is a magician or a swordsman will automatically correspond to the corresponding
 John  

Do you see that the multi-benefits of it?

Code download
http://uploadingit.com/file/hy2pfypcshgyp8c3/StrategyPatternDemo.zip 

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © .Hacking Cracking Tricks And Tutorials, Paid Scripts, Latest Exploits, 0Day Vulnerability, - Skyblue - Powered by Blogger - Designed by Johanes Djogan -