Java的建造模式(Build)

建造模式:

1 产品类:

package  com.javapatterns.builder;

public   class  Product
{
    
public Product()
    
{
       
// System.out.println("product is initing.");
    }

}

2 抽象建造方法:

package  com.javapatterns.builder;

abstract   public   class  Builder
{
    
public abstract void buildPart1();

    
public abstract void buildPart2();

    
public abstract Product retrieveResult();
}

 3 具体的建造类:产品的零件制造

package  com.javapatterns.builder;

public   class  ConcreteBuilder  extends  Builder
{
    
/**
     * @label Creates
     
*/

    
private Product product = new Product() ;

    
public void buildPart1()
    
{
        System.out.println(
"build part one.");
    }


    
public void buildPart2()
    
{
        System.out.println(
"build part two.");
    }


    
public Product retrieveResult()
    
{
        
return product;
    }

}

4 产品的建造过程:

package  com.javapatterns.builder;

public   class  Director
{
    
/**
     * 
@link aggregation
     * @directed
     * @clientRole builder
*/

    
private Builder builder;

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


    
public void construct()
    
{
        builder 
= new ConcreteBuilder();

        builder.buildPart1();

        builder.buildPart2();

        builder.retrieveResult();

        
//continue with other code
    }

}

5 测试:

package  com.javapatterns.builder;

public   class  Client
{
    
/**
     * 
@link aggregation
     * @directed 
     
*/

    
private static Director director;

    
private static Builder builder = new ConcreteBuilder();
        
    
public void requestBuild()
    
{
       
// director = new Director(builder);
    }

    
     
public static void main(String[] args) 
    
{
        director 
= new Director(builder);
        director.construct();
    }


}

 

init:
deps-jar:
compile-single:
run-single:
build part one.
build part two.
生成成功(总时间:0 秒)

你可能感兴趣的:(JavaEE等)