【java设计模式】【创建模式Creational Pattern】建造模式Builder Pattern

 package com.tn.pattern;

 public class Client {
public static void main(String[] args) {
Director director=Director.getInstance();
director.construct(new ConcreteBuilder1());
director.construct(new ConcreteBuilder2());
}
} class Director{
static Director director;
private Director(){}
public static Director getInstance(){
if(director==null)
director=new Director();
return director;
}
public void construct(Builder builder){
builder.buildPart1();
builder.buildPart2();
}
} interface Builder{
void buildPart1();
void buildPart2();
Product retrieveResult();
} class ConcreteBuilder1 implements Builder{
Product product;
@Override
public void buildPart1() {
System.out.println("ConcreteBuilder1.buildPart1()");
product=new Product1();
} @Override
public void buildPart2() {} @Override
public Product retrieveResult() {
return product;
}
} class ConcreteBuilder2 implements Builder{
Product product;
@Override
public void buildPart1() {} @Override
public void buildPart2() {
System.out.println("ConcreteBuilder2.buildPart2()");
product=new Product2();
} @Override
public Product retrieveResult() {
return product;
}
} interface Product{} class Product1 implements Product{
public Product1(){
System.out.println("Product1.Product1()");
}
} class Product2 implements Product{
public Product2(){
System.out.println("Product2.Product2()");
}
}
上一篇:python random从集合中随机选择元素


下一篇:Windows批处理 -- 打造MySQLCleaner