1.简介
将一个复杂的对象的构建与他的表示分离,使得同样的构建过程可以创建不同的表示,这样的设计模式被称为建造者模式。创建者模式主要是为了将对象的构建装配与他的表现分离。譬如生产两种运动鞋阿迪和耐克,虽然生产材料款式不同,但是生产的过程都是将鞋底,鞋面,鞋带装在一起。这样对象就有了共性,我们可以抽象出一个制造者(建造者),将不同鞋的生产过程‘告诉’这个制造者,这样这个制造者就能制造出不同的鞋子。
2.结构
3.示例
//产品对象
public class Shoe { | |
//鞋带 | |
private String shoelace; | |
//鞋底 | |
private String sole; | |
//鞋面 | |
private String vamp; | |
//LOGO | |
private String logo; | |
public String getLogo() { | |
return logo; | |
} | |
public void setLogo(Stringlogo) { | |
this.logo= logo; | |
} | |
public String getShoelace() { | |
return shoelace; | |
} | |
public void setShoelace(Stringshoelace) { | |
this.shoelace= shoelace; | |
} | |
public String getSole() { | |
return sole; | |
} | |
public void setSole(Stringsole) { | |
this.sole= sole; | |
} | |
public String getVamp() { | |
return vamp; | |
} | |
public void setVamp(Stringvamp) { | |
this.vamp= vamp; | |
} | |
} |
public class AdidasShoe extends Shoe { | |
} |
//耐克鞋
public class NikeShoe extends Shoe { | |
} |
//抽象建造者
public interface Builder { | |
public void installShoelace(); | |
public void installSole(); | |
public void installVamp(); | |
public void installLogo(); | |
public Shoe getShoe(); | |
} |
public class AdidasShoeBuilderimplements Builder { | |
private Shoe shoe; | |
public AdidasShoeBuilder() { | |
// TODO Auto-generated constructor stub | |
shoe = new AdidasShoe(); | |
} | |
@Override | |
public void installShoelace() { | |
// TODO Auto-generated method stub | |
shoe.setShoelace("blue shoelace"); | |
} | |
@Override | |
public void installSole() { | |
// TODO Auto-generated method stub | |
shoe.setSole("comfortable sole"); | |
} | |
@Override | |
public void installVamp() { | |
// TODO Auto-generated method stub | |
shoe.setVamp("black vamp"); | |
} | |
@Override | |
public void installLogo() { | |
// TODO Auto-generated method stub | |
shoe.setLogo("ADIDAS"); | |
} | |
public Shoe getShoe() { | |
return shoe; | |
} | |
} |
public class NikeShoeBuilderimplements Builder { | |
private Shoe shoe; | |
public NikeShoeBuilder(){ | |
shoe = new NikeShoe(); | |
} | |
@Override | |
public void installShoelace() { | |
// TODO Auto-generated method stub | |
shoe.setShoelace("red shoelace"); | |
} | |
@Override | |
public void installSole() { | |
// TODO Auto-generated method stub | |
shoe.setSole("comfortable sole"); | |
} | |
@Override | |
public void installVamp() { | |
// TODO Auto-generated method stub | |
shoe.setVamp("black vamp"); | |
} | |
@Override | |
public void installLogo() { | |
// TODO Auto-generated method stub | |
shoe.setLogo("NIKE"); | |
} | |
public Shoe getShoe() { | |
return shoe; | |
} | |
} |
public class ShoeBuilder { | |
private Builder builder ; | |
public ShoeBuilder(Builder builder){ | |
this.builder= builder; | |
} | |
public Builder install(){ | |
builder.installShoelace(); | |
builder.installSole(); | |
builder.installVamp(); | |
builder.installLogo(); | |
return builder; | |
} | |
} |
//主函数
public class Main { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Builder adBuilder = new AdidasShoeBuilder(); | |
Builder nikeBuilder = new NikeShoeBuilder(); | |
ShoeBuilder builder = new ShoeBuilder(nikeBuilder); | |
Shoe nikeShoe = builder.install().getShoe(); | |
System.out.println(" a"+ nikeShoe.getShoelace()+""+ nikeShoe.getLogo()+" shoe"); | |
builder = new ShoeBuilder(adBuilder); | |
Shoe adShoe = builder.install().getShoe(); | |
System.out.println(" a"+ adShoe.getShoelace()+""+ adShoe.getLogo()+" shoe"); | |
} | |
} |