建造者模式主要解決問題: 具備若幹成員,當其中一個成員發生變化,其它成員也隨著發生變化。 這種複雜對象的生成需要使用建造者模式來生成。 建造者設計模式的結構圖: 來源:http://c.biancheng.net/view/1354.html 例子: 街頭籃球角色創建模擬 街頭籃球: 中鋒、前鋒、後 ...
建造者模式主要解決問題:
具備若幹成員,當其中一個成員發生變化,其它成員也隨著發生變化。
這種複雜對象的生成需要使用建造者模式來生成。
建造者設計模式的結構圖:
來源:http://c.biancheng.net/view/1354.html
例子:
街頭籃球角色創建模擬
街頭籃球: 中鋒、前鋒、後衛
最重要的屬性:身高(會影響其他屬性)
中鋒:身高加1 速度減1 蓋帽加2
前鋒:身高加2 速度減1 蓋帽加1
後衛:身高加3 速度減1 蓋帽加1
代碼如下:
代碼結構圖;
具體代碼;
父類;AbstractPlayer:
1 package org.yys.建造者設計模式.interfaces.impl; 2 3 public class AbstractPlayer { 4 /** 5 * 身高: height 6 * 速度: speedvalue 7 * 蓋帽: blockvalue 8 */ 9 10 protected int height; 11 protected int speedvalue; 12 protected int blockvalue; 13 public int getHeight() { 14 return height; 15 } 16 public void setHeight(int height) { 17 this.height = height; 18 } 19 public int getSpeedvalue() { 20 return speedvalue; 21 } 22 public void setSpeedvalue(int speedvalue) { 23 this.speedvalue = speedvalue; 24 } 25 public int getBlockvalue() { 26 return blockvalue; 27 } 28 public void setBlockvalue(int blockvalue) { 29 this.blockvalue = blockvalue; 30 } 31 @Override 32 public String toString() { 33 return "Player [height=" + height + ", speedvalue=" + speedvalue + ", blockvalue=" + blockvalue + "]"; 34 } 35 36 37 38 }
CPlayer:前鋒entity:
1 package org.yys.建造者設計模式.entity; 2 3 import org.yys.建造者設計模式.interfaces.impl.AbstractPlayer; 4 public class CPlayer extends AbstractPlayer{ 5 6 public CPlayer() { 7 super(); 8 this.height=190; 9 this.speedvalue=25; 10 this.blockvalue=75 ; 11 } 12 13 @Override 14 public String toString() { 15 return "CPlayer [toString()=" + super.toString() + "]"; 16 } 17 18 19 }
FPlayer:中鋒entity:
1 package org.yys.建造者設計模式.entity; 2 import org.yys.建造者設計模式.interfaces.impl.AbstractPlayer; 3 4 public class FPlayer extends AbstractPlayer{ 5 6 @Override 7 public String toString() { 8 return "FPlayer [toString()=" + super.toString() + "]"; 9 } 10 11 public FPlayer() { 12 super(); 13 this.height=180 ; 14 this.blockvalue=50 ; 15 this.speedvalue=50 ; 16 } 17 }
GPlayer:後衛entity:
1 package org.yys.建造者設計模式.entity; 2 3 import org.yys.建造者設計模式.interfaces.impl.AbstractPlayer; 4 5 public class GPlayer extends AbstractPlayer { 6 7 public GPlayer() { 8 super(); 9 this.height = 170 ; 10 this.blockvalue = 30 ; 11 this.speedvalue =75 ; 12 } 13 14 @Override 15 public String toString() { 16 return "GPlayer [toString()=" + super.toString() + "]"; 17 } 18 19 20 21 }
Builder介面(改變數值的具體方法介面)
1 package org.yys.建造者設計模式.interfaces; 2 3 import org.yys.建造者設計模式.interfaces.impl.AbstractPlayer; 4 5 public interface Builder { 6 7 void changeHeight(int count); 8 void changeBlockValue(int count); 9 void changeSpeedValue(int count); 10 11 AbstractPlayer getPlayer(); 12 }
CPlayerBuilderImpl:
1 package org.yys.建造者設計模式.interfaces.impl; 2 3 import org.yys.建造者設計模式.entity.CPlayer; 4 import org.yys.建造者設計模式.interfaces.Builder; 5 6 public class CPlayerBuilderImpl implements Builder { 7 8 private CPlayer player = new CPlayer(); 9 @Override 10 public void changeHeight(int count) { 11 12 this.player.height +=count ; 13 } 14 15 @Override 16 public void changeBlockValue(int count) { 17 // TODO Auto-generated method stub 18 this.player.blockvalue +=count*2; 19 } 20 21 @Override 22 public void changeSpeedValue(int count) { 23 // TODO Auto-generated method stub 24 this.player.speedvalue -=count ; 25 } 26 27 @Override 28 public AbstractPlayer getPlayer() { 29 // TODO Auto-generated method stub 30 return this.player; 31 } 32 33 }
FPlayerBuilderImpl:
1 package org.yys.建造者設計模式.interfaces.impl; 2 3 import org.yys.建造者設計模式.entity.FPlayer; 4 import org.yys.建造者設計模式.interfaces.Builder; 5 6 public class FPlayerBuilderImpl implements Builder { 7 private FPlayer player = new FPlayer(); 8 @Override 9 public void changeHeight(int count) { 10 // TODO Auto-generated method stub 11 this.player.height += count ; 12 } 13 14 @Override 15 public void changeBlockValue(int count) { 16 // TODO Auto-generated method stub 17 this.player.blockvalue +=count/2 ; 18 } 19 20 @Override 21 public void changeSpeedValue(int count) { 22 // TODO Auto-generated method stub 23 this.player.speedvalue -=count/2 ; 24 } 25 26 @Override 27 public AbstractPlayer getPlayer() { 28 // TODO Auto-generated method stub 29 return this.player; 30 } 31 32 }
GPlayerBuilderImpl:
1 package org.yys.建造者設計模式.interfaces.impl; 2 3 import org.yys.建造者設計模式.entity.GPlayer; 4 import org.yys.建造者設計模式.interfaces.Builder; 5 6 public class GPlayerBuilderImpl implements Builder { 7 private GPlayer player = new GPlayer(); 8 9 @Override 10 public void changeHeight(int count) { 11 // TODO Auto-generated method stub 12 this.player.height += count; 13 } 14 15 @Override 16 public void changeBlockValue(int count) { 17 // TODO Auto-generated method stub 18 this.player.blockvalue += count / 3; 19 } 20 21 @Override 22 public void changeSpeedValue(int count) { 23 // TODO Auto-generated method stub 24 this.player.speedvalue -= count / 3; 25 } 26 27 @Override 28 public AbstractPlayer getPlayer() { 29 // TODO Auto-generated method stub 30 return this.player; 31 } 32 33 }
Redirector(總指揮)
1 package org.yys.建造者設計模式.interfaces; 2 3 import org.yys.建造者設計模式.interfaces.impl.AbstractPlayer; 4 5 public class Redirector { 6 7 private Builder builder; 8 9 public Redirector(Builder builder) { 10 super(); 11 this.builder = builder; 12 } 13 14 public AbstractPlayer changeCount(int count) { 15 16 builder.changeHeight(count); 17 builder.changeSpeedValue(count); 18 builder.changeBlockValue(count); 19 20 return builder.getPlayer(); 21 22 } 23 24 }
測試:
1 package org.yys.建造者設計模式.Demo; 2 3 import org.yys.建造者設計模式.interfaces.Redirector; 4 import org.yys.建造者設計模式.interfaces.impl.AbstractPlayer; 5 import org.yys.建造者設計模式.interfaces.impl.CPlayerBuilderImpl; 6 7 public class TestDemo { 8 public static void main(String[] args) { 9 10 Redirector redirector = new Redirector(new CPlayerBuilderImpl()); 11 12 AbstractPlayer player =redirector.changeCount(10); 13 14 System.out.println(player); 15 16 17 } 18 }
結果:
學識淺薄,如有不對,懇請指教。