package designpatten;
/**
* 创建者模式
*/
abstract class IComputerBuider{
/**
* 模拟复杂对象
*/
public String mainBoard;
public String audioCard;
public String showCard;
/**
* 复杂对象创建接口
* 每个复杂对象的创建可能都很复杂,会发生变化
*/
public abstract void createMainBoard();
public abstract void createAudioCard();
public abstract void createShowCard();
public void describe(){
StringBuilder sb=new StringBuilder();
sb.append("主板:").append(mainBoard).append(",").append("声卡:").append(audioCard).append(",").append("显卡:").append(showCard);
System.out.println(sb.toString());
}
}
class IBMComputerBuider extends IComputerBuider{
public void createMainBoard() {
this.mainBoard="三星";
}
public void createAudioCard() {
this.audioCard="IBM";
}
public void createShowCard() {
this.showCard="华硕";
}
public void describe(){
System.out.println("IBM电脑配置:");
super.describe();
}
}
class HPComputerBuider extends IComputerBuider{
public void createMainBoard() {
this.mainBoard="IBM";
}
public void createAudioCard() {
this.audioCard="三星";
}
public void createShowCard() {
this.showCard="七彩虹";
}
public void describe(){
System.out.println("HP电脑配置:");
super.describe();
}
}
/**
* 封装了对象创建过程
* 也可以声明Director为接口,通过具体子类改变复杂对象的创建顺序
*/
class Director{
private IComputerBuider builder;
public Director(IComputerBuider builder){
this.builder=builder;
}
public IComputerBuider getBuilder() {
return builder;
}
public void setBuilder(IComputerBuider builder) {
this.builder = builder;
}
public void build(){
builder.createMainBoard();
builder.createAudioCard();
builder.createShowCard();
builder.describe();
}
}
public class Builder {
public static void main(String [] args){
IComputerBuider builder=new HPComputerBuider();
Director director=new Director(builder);
director.build();
director.setBuilder(new IBMComputerBuider());
director.build();
}
}
/**
运行结果:
HP电脑配置:
主板:IBM,声卡:三星,显卡:七彩虹
IBM电脑配置:
主板:三星,声卡:IBM,显卡:华硕
*/
/**
* 创建者模式
*/
abstract class IComputerBuider{
/**
* 模拟复杂对象
*/
public String mainBoard;
public String audioCard;
public String showCard;
/**
* 复杂对象创建接口
* 每个复杂对象的创建可能都很复杂,会发生变化
*/
public abstract void createMainBoard();
public abstract void createAudioCard();
public abstract void createShowCard();
public void describe(){
StringBuilder sb=new StringBuilder();
sb.append("主板:").append(mainBoard).append(",").append("声卡:").append(audioCard).append(",").append("显卡:").append(showCard);
System.out.println(sb.toString());
}
}
class IBMComputerBuider extends IComputerBuider{
public void createMainBoard() {
this.mainBoard="三星";
}
public void createAudioCard() {
this.audioCard="IBM";
}
public void createShowCard() {
this.showCard="华硕";
}
public void describe(){
System.out.println("IBM电脑配置:");
super.describe();
}
}
class HPComputerBuider extends IComputerBuider{
public void createMainBoard() {
this.mainBoard="IBM";
}
public void createAudioCard() {
this.audioCard="三星";
}
public void createShowCard() {
this.showCard="七彩虹";
}
public void describe(){
System.out.println("HP电脑配置:");
super.describe();
}
}
/**
* 封装了对象创建过程
* 也可以声明Director为接口,通过具体子类改变复杂对象的创建顺序
*/
class Director{
private IComputerBuider builder;
public Director(IComputerBuider builder){
this.builder=builder;
}
public IComputerBuider getBuilder() {
return builder;
}
public void setBuilder(IComputerBuider builder) {
this.builder = builder;
}
public void build(){
builder.createMainBoard();
builder.createAudioCard();
builder.createShowCard();
builder.describe();
}
}
public class Builder {
public static void main(String [] args){
IComputerBuider builder=new HPComputerBuider();
Director director=new Director(builder);
director.build();
director.setBuilder(new IBMComputerBuider());
director.build();
}
}
/**
运行结果:
HP电脑配置:
主板:IBM,声卡:三星,显卡:七彩虹
IBM电脑配置:
主板:三星,声卡:IBM,显卡:华硕
*/