建造者模式的本质
一般与工厂模式搭配使用
1.分离了对象子组件的单独构造(有bulider来负责)和装配(由Director负责)。从而可以构造出复杂的对象:某个对象构建相对复杂的情况下使用。
2.由于实现了构建和装配的解耦。不同的的解耦器,相同的装配,也可以做出不同的对象;
相同的构造器,不同的装配顺序也可以做出不同的对象。也就实现了
构建算法、装配算法的解耦,实现了更好的复用。
代码实现
代码逻辑分块
1.产品类-(组件类)
2.建造者(创建一个产品对象的各个部件的抽象接口) 具体建造者(实现建造者接口,构建和装配各个部件)
3.指挥者类(构建一个使用建造者接口的对象,调用创建类,创建组件,按照不同的顺序组装成不同的产品)
传统实现方式
产品类
package com.bjsxt.builder.ji;
/**
* @Author JJL
* @Date 2020/9/11 14:48
* @Version 1.0
*/
//飞船类 -- 产品类
public class JiShip {
private OrbitalModule orbitalModule; //轨道舱
private Engine engine; //发动机
private EscapeTower escapeTower; //逃逸塔
public void launch(){
System.out.println("老子要发射!");
}
public OrbitalModule getOrbitalModule() {
return orbitalModule;
}
public void setOrbitalModule(OrbitalModule orbitalModule) {
this.orbitalModule = orbitalModule;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public EscapeTower getEscapeTower() {
return escapeTower;
}
public void setEscapeTower(EscapeTower escapeTower) {
this.escapeTower = escapeTower;
}
}
//轨道舱
class OrbitalModule{
private String name;
public OrbitalModule(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//发动机
class Engine {
private String name;
public Engine(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//逃逸仓
class EscapeTower{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public EscapeTower(String name) {
super();
this.name = name;
}
}
建造者
package com.bjsxt.builder.ji;
import com.bjsxt.builder.ji.Engine;
import com.bjsxt.builder.ji.EscapeTower;
import com.bjsxt.builder.ji.OrbitalModule;
/**
* @Author JJL
* @Date 2020/9/11 14:48
* @Version 1.0
*/
public interface IJiShipBuilder {
/**
* BuildPart
* @return
*/
Engine builderEngine();
OrbitalModule builderOrbitalModule();
EscapeTower builderEscapeTower();
/**
* getResult
* @return
*/
JiShip buildShip();
}
package com.bjsxt.builder.ji;
/**
* @Author JJL
* @Date 2020/9/11 15:14
* @Version 1.0
*/
public class ConJiShipBudiler implements IJiShipBuilder{
private JiShip jiShip = null;
private Engine engine = null;
private EscapeTower escapeTower = null;
private OrbitalModule orbitalModule = null;
/**
* 构造器 -- 这里进行取值
*/
public ConJiShipBudiler() {
if(jiShip == null){
jiShip = new JiShip();
}
}
/**
* buildPart
* @return
*/
@Override
public Engine builderEngine() {
engine = new Engine("老子的发动机");
return engine;
}
/**
* buildPart
* @return
*/
@Override
public OrbitalModule builderOrbitalModule() {
orbitalModule = new OrbitalModule("老子的轨道舱");
return orbitalModule;
}
/**
* buildPart
* @return
*/
@Override
public EscapeTower builderEscapeTower() {
escapeTower = new EscapeTower("老子的逃逸塔");
return escapeTower;
}
/**
* getResult -- 建造者 -- 组装过程
* @return
*/
@Override
public JiShip buildShip() {
jiShip.setEngine(engine);
jiShip.setEscapeTower(escapeTower);
jiShip.setOrbitalModule(orbitalModule);
return jiShip;
}
}
指挥者
package com.bjsxt.builder.ji;
/**
* @Author JJL
* @Date 2020/9/11 16:01
* @Version 1.0
*/
public class Director {
/**
* 用来指挥建造过程
*/
public JiShip Construct(IJiShipBuilder builder) {
builder.builderEngine();
builder.builderEscapeTower();
builder.builderOrbitalModule();
return builder.buildShip();
}
}
调用者
package com.bjsxt.builder.ji;
/**
* @Author JJL
* @Date 2020/9/11 19:39
* @Version 1.0
*/
public class Cilent {
public static void main(String[] args) {
Director director = new Director();
JiShip jiShip = director.Construct(new ConJiShipBudiler());
System.out.println(jiShip.getEngine().getName() + "" + jiShip.getEscapeTower().getName() + jiShip.getOrbitalModule().getName());
jiShip.launch();
}
}
GOF实现方式
产品以及产品组件
//飞船类
public class AirShip {
private OrbitalModule orbitalModule; //轨道舱
private Engine engine; //发动机
private EscapeTower escapeTower; //逃逸塔
public void launch(){
System.out.println("发射!");
}
public OrbitalModule getOrbitalModule() {
return orbitalModule;
}
public void setOrbitalModule(OrbitalModule orbitalModule) {
this.orbitalModule = orbitalModule;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public EscapeTower getEscapeTower() {
return escapeTower;
}
public void setEscapeTower(EscapeTower escapeTower) {
this.escapeTower = escapeTower;
}
}
//轨道舱
class OrbitalModule{
private String name;
public OrbitalModule(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//发动机
class Engine {
private String name;
public Engine(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//逃逸仓
class EscapeTower{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public EscapeTower(String name) {
super();
this.name = name;
}
}
建造者 具体建造者 块
public interface AirShipBuilder {
Engine builderEngine();
OrbitalModule builderOrbitalModule();
EscapeTower builderEscapeTower();
}
//构建者--创建具体的产品组件
public class SxtAirShipBuilder implements AirShipBuilder {
//StringBuilder, 以后学习XML解析中,JDOM库中的类:DomBuilder,SaxBuilder
/**
* 创建方法的返回实例可以跟单例模式和工厂模式结合起来
* @return
*/
@Override
public Engine builderEngine() {
System.out.println("构建尚学堂牌发动机!");
return new Engine("尚学堂牌发动机!");
}
@Override
public EscapeTower builderEscapeTower() {
System.out.println("构建逃逸塔");
return new EscapeTower("尚学堂牌逃逸塔");
}
@Override
public OrbitalModule builderOrbitalModule() {
System.out.println("构建轨道舱");
return new OrbitalModule("尚学堂牌轨道舱");
}
}
指挥者块
//装配者(装配组件返回产品)
public interface AirShipDirector {
/**
* 组装飞船对象
* @return
*/
AirShip directAirShip();
}
/**
* 装配者,将构建者传进来之后,按照顺序等(装配过程)装配成具体的产品 -- 组装
*/
public class SxtAirshipDirector implements AirShipDirector {
private AirShipBuilder builder;
public SxtAirshipDirector(AirShipBuilder builder) {
this.builder = builder;
}
@Override
public AirShip directAirShip() {
Engine e = builder.builderEngine();
OrbitalModule o = builder.builderOrbitalModule();
EscapeTower et = builder.builderEscapeTower();
//装配成飞船对象
AirShip ship = new AirShip();
ship.setEngine(e);
ship.setEscapeTower(et);
ship.setOrbitalModule(o);
return ship;
}
}
调用块
public class Client {
public static void main(String[] args) {
AirShipDirector director = new SxtAirshipDirector(new SxtAirShipBuilder());
AirShip ship = director.directAirShip();
System.out.println(ship.getEngine().getName());
ship.launch();
}
}
变种
具体建造者
public class ExtendByJiShipBuilder implements AirShipBuilder{
@Override
public Engine builderEngine() {
System.out.println("构建发动机X2");
return new Engine("X2");
}
@Override
public OrbitalModule builderOrbitalModule() {
System.out.println("构建轨道舱X2");
return new OrbitalModule("X2");
}
@Override
public EscapeTower builderEscapeTower() {
System.out.println("构建逃逸塔X2");
return new EscapeTower("X2");
}
}
指挥者类
public class ExtendByJiShipDirector implements AirShipDirector{
private AirShipBuilder builder;
public void setBuilder(AirShipBuilder b){
this.builder = b;
}
@Override
public AirShip directAirShip() {
AirShip ship = new AirShip();
ship.setEngine(builder.builderEngine());
ship.setEscapeTower(builder.builderEscapeTower());
ship.setOrbitalModule(builder.builderOrbitalModule());
return ship;
}
}
调用者
public class Client {
public static void main(String[] args) {
AirShipDirector director = new SxtAirshipDirector(new SxtAirShipBuilder());
AirShip ship = director.directAirShip();
System.out.println(ship.getEngine().getName());
ship.launch();
//变种
ExtendByJiShipDirector extendByJiShipDirector = new ExtendByJiShipDirector();
ExtendByJiShipBuilder builder = new ExtendByJiShipBuilder();
extendByJiShipDirector.setBuilder(builder);
AirShip eShip = extendByJiShipDirector.directAirShip();
System.out.println(eShip.getEngine().getName());
eShip.launch();
}
}
使用场景
1.StringBuilder类的append方法
StringBuilder继承了AbstractStringBuilder,而AbstractStringBuilder实现了appendable
StringBuilder:指挥者类,持有具体建造者的引用,由于StringBuilder继承了AbstractStringBuilder,这里StringBuilder通过super来作为具体建造者的引用。
AbstractStringBuilder:具体建造者,它实现了appendable接口的append(Character c)方法。
appendable:抽象建造者,定义了创建对象的接口。
public StringBuilder(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
@Override
public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
@Override
public StringBuilder append(String str) {
super.append(str);
return this;
}
2.SQL中的PreparedStatement
3.JDOM中,DomBuilder、SAXBuilder
SAXBuilder
SAXBuilder builder=new SAXBuilder(false);
Reader returnQuote = new StringReader(rexml);
Document doc = builder.build(returnQuote);
得到根元素:
Element books=doc.getRootElement();
在JDOM中所有的节点(DOM中的概念)都是一个org.jdom.Element 类,当然他的子节点也是一个org.jdom.Element类。
得到元素(节点)的集合:
List booklist=books.getChildren(“book”);
DomBuilder
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Documentdoc=builder.newDocument();
Element root=doc.createElement("root");
Text text=doc.createText("Thisistheroot");
root.appendChild(text);
doc.appendChild(root);
DOMBuilder builder=new DOMBuilder();
Document doc=builder.build(newFile("jdom_test.xml"));
SAXBuilder builder=new SAXBuilder();
Document doc=builder.build(url);