设计模式模式(四):建造者模式(生成器模式)
建造者模式主要解决问题:
具备若干成员,当其中一个成员发生变化,其它成员也随着发生变化。
这种复杂对象的生成需要使用建造者模式来生成。
建造者设计模式的结构图:
来源: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 }
结果:
学识浅薄,如有不对,恳请指教。
赞 (0)