建造者模式听上去是个有点抽象的概念,但是,只要是做过 Web 开发的基本上都应该用过,只是可能自己不知道这就是建造者模式,我们来学习学习...
建造者模式
建造者模式(Builder Pattern):是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。建造者模式属于创建型模式,对使用者而言,只需要指定需要建造的类型就可以获得对象,建造过程和细节不需要了解。
示例:那今天我们就以家庭作业为例,假设老师会根据每个不同基础的同学布置不同难度的题目来实现一个简单的建造者模式。
下面我们先来个简单的案例,感受一下建造者模式:
package cn.liangyy.builder.easy;
/**
* 家庭作业类
*/
public class HomeWork {
//简单题目
private String easyQc;
//正常题目
private String normalQc;
//中等难度题目
private String MediumQc;
//困难题目
private String HardQc;
public String getEasyQc() {
return easyQc;
}
public void setEasyQc(String easyQc) {
this.easyQc = easyQc;
}
public String getNormalQc() {
return normalQc;
}
public void setNormalQc(String normalQc) {
this.normalQc = normalQc;
}
public String getMediumQc() {
return MediumQc;
}
public void setMediumQc(String mediumQc) {
MediumQc = mediumQc;
}
public String getHardQc() {
return HardQc;
}
public void setHardQc(String harQc) {
HardQc = harQc;
}
}
复制代码
package cn.liangyy.builder.easy;
/**
* 建造者类
* 用来创建对象
*/
public class SimpleBuilder {
private HomeWork homeWork;
public SimpleBuilder(HomeWork homeWork) {
this.homeWork = homeWork;
}
public void buildEasyQc(String easyQc){
homeWork.setEasyQc(easyQc);
}
public void buildNormalQc(String normalQc){
homeWork.setNormalQc(normalQc);
}
public void buildMediumQc(String mediumQc){
homeWork.setMediumQc(mediumQc);
}
public void buildHardQc(String hardQc){
homeWork.setHardQc(hardQc);
}
public HomeWork build(){
return homeWork;
}
}
复制代码
package cn.liangyy.builder.easy;
/**
* 建造者模式-测试
*/
public class TestSimpleBuilder {
public static void main(String[] args) {
SimpleBuilder simpleBuilder = new SimpleBuilder(new HomeWork());
simpleBuilder.buildEasyQc("简单题目");
simpleBuilder.buildNormalQc("标准难度题目");
simpleBuilder.buildMediumQc("中等难度题目");
simpleBuilder.buildHardQc("高难度题目");
HomeWork homeWork = simpleBuilder.build();
StringBuilder sb = new StringBuilder();
sb.append(null == homeWork ? "" : homeWork.getEasyQc()+",");
sb.append(null == homeWork ? "" : homeWork.getNormalQc()+",");
sb.append(null == homeWork ? "" : homeWork.getMediumQc()+",");
sb.append(null == homeWork ? "" : homeWork.getHardQc()+"!");
System.out.println("我的家庭作业有:"+sb.toString());
}
}
复制代码
上面就是一个简单的建造者模式案例,题目难度可任意控制,这也是建造者模式的灵活性所在。但是这是直接面向了具体的建造者来编程的,在以后的扩展会比较困难,而我们一般都是选择面对对象编程,下面就来看一下标准的建造者模式写法:
package cn.liangyy.builder.easy;
/**
* 家庭作业类
*/
public class HomeWork {
//简单题目
private String easyQc;
//正常题目
private String normalQc;
//中等难度题目
private String MediumQc;
//困难题目
private String HardQc;
public String getEasyQc() {
return easyQc;
}
public void setEasyQc(String easyQc) {
this.easyQc = easyQc;
}
public String getNormalQc() {
return normalQc;
}
public void setNormalQc(String normalQc) {
this.normalQc = normalQc;
}
public String getMediumQc() {
return MediumQc;
}
public void setMediumQc(String mediumQc) {
MediumQc = mediumQc;
}
public String getHardQc() {
return HardQc;
}
public void setHardQc(String harQc) {
HardQc = harQc;
}
}
复制代码
package cn.liangyy.builder.norm;
/**
* 抽象的建造者类
*/
public abstract class HomeWorkBuilder {
public abstract HomeWorkBuilder builderEasyQc(String easyQc);
public abstract HomeWorkBuilder builderNormQc(String normQc);
public abstract HomeWorkBuilder builderMediumQc(String mediumQc);
public abstract HomeWorkBuilder builderHardQc(String hardQc);
public abstract HomeWork build();
}
复制代码
package cn.liangyy.builder.norm;
/**
* 具体的建造者类
*/
public class ConcreateBuilder extends HomeWorkBuilder {
private HomeWork homeWork;
public ConcreateBuilder(HomeWork homeWork) {
this.homeWork = homeWork;
}
@Override
public HomeWorkBuilder builderEasyQc(String easyQc) {
homeWork.setEasyQc(easyQc);
return this;
}
@Override
public HomeWorkBuilder builderNormQc(String normQc) {
homeWork.setNormalQc(normQc);
return this;
}
@Override
public HomeWorkBuilder builderMediumQc(String mediumQc) {
homeWork.setMediumQc(mediumQc);
return this;
}
@Override
public HomeWorkBuilder builderHardQc(String hardQc) {
homeWork.setHardQc(hardQc);
return this;
}
@Override
public HomeWork build() {
return null;
}
}
复制代码
package cn.liangyy.builder.norm;
/**
* 建造者模式-测试
*/
public class TestBuilder {
public static void main(String[] args) {
HomeWork homeWork = new HomeWork();
HomeWorkBuilder homeWorkBuilder = new ConcreateBuilder(homeWork);
homeWorkBuilder.builderEasyQc("我是一道简单题目")
.builderNormQc("我是一道标准题目")
.builderMediumQc("我是一道中等难度题目")
.builderHardQc("我是一道困难题目");
StringBuilder sb = new StringBuilder();
sb.append(null == homeWork.getEasyQc() ? "" : homeWork.getEasyQc()+",");
sb.append(null == homeWork.getNormalQc() ? "" : homeWork.getNormalQc()+",");
sb.append(null == homeWork.getMediumQc() ? "" : homeWork.getMediumQc()+",");
sb.append(null == homeWork.getHardQc() ? "" : homeWork.getHardQc()+"!");
System.out.println("我的家庭作业有:"+sb.toString());
}
}
复制代码
建造者模式使用场景适用于一个具有较多属性的复杂产品创建过程,而且产品的各个属性还需要支持动态变化,但属性的种类却总体稳定的场景。所以建造者和工厂模式的区别也很明显,工厂模式是返回一个完整的产品,不支持自由组合属性,而建造者更加灵活,完全可以由使用者自由搭配从而使同一个建造者可以建造出不同的产品。
建造者模式优点
封装性好,创建和使用分离
扩展性好,各个建造者类之间相互独立,实现解耦
建造者模式缺点
评论