创建者模式

package com.sise.llm.test;


//建造者模式:将一个复杂对象的建造与他的表示分离,使得同样的构造过程可以创建不同的表示
//四部分 Director(步骤控制) ConcreteBuilder(具体构建方式) Builder(抽象产品) Product(产品A)
// ProductBuilder(抽象接口:产品属性与创建方法)
//
// ProductBuilder DefaultConcreteProductBuilder Director Product
// 想法-》工具-》实现过程-》产品
//
//
// 1
public class BuilderTest {
public static void main(String[] args) {
//Product a = new Product("产品A","公司1","功能1");
DefaultConcreteProductBuilder defaultConcreteProductBuilder = new DefaultConcreteProductBuilder();
Director director = new Director(defaultConcreteProductBuilder);
director.makeProduct("产品名称","公司名称","功能1");



}
}

interface ProductBuilder{
void builderProductName(String productName);
void builderCompanyName(String companyName);
void builderPart1(String part1);

Product builder();
}

class DefaultConcreteProductBuilder implements ProductBuilder{
private String productName;
private String companyName;
private String part1;

@Override
public void builderProductName(String productName) {
this.productName = productName;

}

@Override
public void builderCompanyName(String companyName) {
this.companyName = companyName;

}

@Override
public void builderPart1(String part1) {
this.part1 = part1;

}

@Override
public Product builder() {
return new Product(this.productName,this.companyName,this.part1);
}
}

class Director{
private ProductBuilder builder;

public Director(ProductBuilder builder){
this.builder = builder;
}

public Product makeProduct(String productName, String companyName, String part1){
builder.builderProductName(productName);
builder.builderCompanyName(companyName);
builder.builderPart1(part1);
Product product = builder.builder();
System.out.print(product);
return product;
}
}

class Product{

private String productName;
private String companyName;
private String part1;



public Product(String productName, String companyName, String part1) {
this.productName = productName;
this.companyName = companyName;
this.part1 = part1;
}

public String getProductName() {
return productName;
}

public void setProductName(String productName) {
this.productName = productName;
}

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

public String getPart1() {
return part1;
}

public void setPart1(String part1) {
this.part1 = part1;
}




}
posted @ 2019-12-15 17:02  llm111222333  阅读(136)  评论(0编辑  收藏  举报