package com.designpattern.builderpattern;
/**
* 高个子的人
*
* @author zhongtao on 2018/9/17
*/
public class tallpersonbuilder implements builderhuman {
human human;
public tallpersonbuilder() {
human = new human();
}
@override
public void buildhead() {
human.sethead("普通的头脑");
}
@override
public void buildbody() {
human.setbody("壮实,高大的身体");
}
@override
public void buildhand() {
human.sethand("长手");
}
@override
public void buildfoot() {
human.setfoot("长脚");
}
@override
public human createhuman() {
return human;
}
}
smarthuman
package com.designpattern.builderpattern;
/**
* 聪明的人
*
* @author zhongtao on 2018/9/17
*/
public class smarthumanbuilder implements builderhuman {
human human;
public smarthumanbuilder() {
human = new human();
}
@override
public void buildhead() {
human.sethead("高智商的头脑");
}
@override
public void buildbody() {
human.setbody("健康的身体");
}
@override
public void buildhand() {
human.sethand("普通的手");
}
@override
public void buildfoot() {
human.setfoot("普通的脚");
}
@override
public human createhuman() {
return human;
}
}
4、director 建造者模式的核心 调用具体建造者来创建不同的人
package com.designpattern.builderpattern;
/**
* 管理造人的顺序 builderhuman不同,则创建的人不同
* @author zhongtao on 2018/9/17
*/
public class humandirector {
public human createhumanbydirector(builderhuman builderhuman){
builderhuman.buildhead();
builderhuman.buildbody();
builderhuman.buildhand();
builderhuman.buildfoot();
return builderhuman.createhuman();
}
}
5、建造者模式测试
package com.designpattern.builderpattern;
import org.junit.test;
/**
* 测试
*
* @author zhongtao on 2018/9/17
*/
public class builderpatterntest {
/**
* 测试建造者模式
*/
@test
public void test() {
humandirector humandirector = new humandirector();
//创建高个子的人
human humanbydirector = humandirector.createhumanbydirector(new tallpersonbuilder());
system.out.println(humanbydirector.gethead());
system.out.println(humanbydirector.getbody());
system.out.println(humanbydirector.gethand());
system.out.println(humanbydirector.getfoot());
system.out.println("------简单的分割线------");
//创建聪明的人
human smarthuman = humandirector.createhumanbydirector(new smarthumanbuilder());
system.out.println(smarthuman.gethead());
system.out.println(smarthuman.getbody());
system.out.println(smarthuman.gethand());
system.out.println(smarthuman.getfoot());
}
}