首页 > 代码库 > 设计模式--策略模式

设计模式--策略模式

策略模式,良好的扩展性,将对象分割成2部分,一部分是相同属性(抽象类),一部分是独有的属性(接口),良好的扩展,自由的组合出需要的新的对象

资料来自网络,例子由自己编写

例子:  角色和武器   角色都有攻击力 ,攻击速度和移动速度    武器有自己独有的特性     但是角色又拥有武器

定义角色的抽象父类

 1 package org.masque.designpatterns.strategy; 2 /** 3  *  4  * Description: 5  * Player.java Create on 2014年7月5日 下午1:39:16  6  * @author masque.java@outlook.com 7  * @version 1.0 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9  */10 public abstract class Player {11 12     protected int ATK;13     14     protected double ASPD;15     16     protected int speed;17     18     protected Weapon weapon;19 20     public int getATK() {21         return ATK;22     }23 24     public void setATK(int aTK) {25         ATK = aTK;26     }27 28     public double getASPD() {29         return ASPD;30     }31 32     public void setASPD(double aSPD) {33         ASPD = aSPD;34     }35 36     public int getSpeed() {37         return speed;38     }39 40     public void setSpeed(int speed) {41         this.speed = speed;42     }43 44     public Weapon getWeapon() {45         return weapon;46     }47 48     public void setWeapon(Weapon weapon) {49         this.weapon = weapon;50     }51 52     @Override53     public String toString() {54         return "Player [ATK=" + ATK + ", ASPD=" + ASPD + ", speed=" + speed55                 + ", weapon=" + weapon + "]";56     }57 }

定义武器的接口:

 1 package org.masque.designpatterns.strategy; 2 /** 3  *  4  * Description: 5  * Weapon.java Create on 2014年7月5日 下午1:39:16  6  * @author masque.java@outlook.com 7  * @version 1.0 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9  */10 public interface Weapon {11 12     public void display();13 }

角色玩家:

 1 package org.masque.designpatterns.strategy; 2 /** 3  *  4  * Description: 5  * Player01.java Create on 2014年7月5日 下午1:39:16  6  * @author masque.java@outlook.com 7  * @version 1.0 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9  */10 public class Player01 extends Player {11 12     public Player01(int ATK,double ASPD,int speed,Weapon weapon){13         this.ATK = ATK;14         this.ASPD = ASPD;15         this.speed = speed;16         this.weapon = weapon;17     }18 }

武器1:

 1 package org.masque.designpatterns.strategy; 2 /** 3  *  4  * Description: 5  * Weapon01.java Create on 2014年7月5日 下午1:39:16  6  * @author masque.java@outlook.com 7  * @version 1.0 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9  */10 public class Weapon01 implements Weapon {11 12     @Override13     public void display() {14         System.out.println("我是武器01");15     }16 17     @Override18     public String toString() {19         display();20         return "Weapon01 []";21     }22 }

武器2:

 1 package org.masque.designpatterns.strategy; 2 /** 3  *  4  * Description: 5  * Weapon02.java Create on 2014年7月5日 下午1:39:16  6  * @author masque.java@outlook.com 7  * @version 1.0 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9  */10 public class Weapon02 implements Weapon {11 12     @Override13     public void display() {14         System.out.println("我是武器02");15     }16 17     @Override18     public String toString() {19         display();20         return "Weapon02 []";21     }22 }

测试类:

 1 package org.masque.designpatterns.strategy; 2 /** 3  *  4  * Description: 5  * MainTest.java Create on 2014年7月5日 下午1:39:16  6  * @author masque.java@outlook.com 7  * @version 1.0 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9  */10 public class MainTest {11 12     public static void main(String[] args) {13         Player01 player01 = new Player01(50, 0.65, 350, new Weapon01());14         System.out.println(player01);15         16         player01.setWeapon(new Weapon02());17         System.out.println(player01);18     }19 }

player不需要知道Weapon具体的实现细节

java多态,player拥有的武器也能更换