设计模式模式(四):建造者模式(生成器模式)
程序员文章站
2023-11-19 16:33:16
建造者模式主要解决问题: 具备若干成员,当其中一个成员发生变化,其它成员也随着发生变化。 这种复杂对象的生成需要使用建造者模式来生成。 建造者设计模式的结构图: 来源: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 }
结果:
学识浅薄,如有不对,恳请指教。
上一篇: mybatis中的动态SQL语句