码迷,mamicode.com
首页 > 其他好文 > 详细

创建者模式学习

时间:2019-12-15 18:14:29      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:创建   return   建造者   步骤   ack   system   set   ISE   tco   

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;
}




}

创建者模式学习

标签:创建   return   建造者   步骤   ack   system   set   ISE   tco   

原文地址:https://www.cnblogs.com/oo-llm/p/12045015.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!