設計模式--策略模式

來源:互聯網
上載者:User

標籤:des   style   blog   http   java   color   

策略模式,良好的擴充性,將對象分割成2部分,一部分是相同屬性(抽象類別),一部分是專屬的屬性(介面),良好的擴充,自由的組合出需要的新的對象

資料來自網路,例子由自己編寫

例子:  角色和武器   角色都有攻擊力 ,攻擊速度和移動速度    武器有自己專屬的特性     但是角色又擁有武器

定義角色的抽象父類

 1 package org.masque.designpatterns.strategy; 2 /** 3  *  4  * Description: 5  * Player.java Create on 2014年7月5日 下午1:39:16  6  * @author [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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擁有的武器也能更換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.