设计模式--生成器模式

概述

  将复杂对象的构建与表示分离,使得同样的构建过程可以创建不同的表示。

 

适用场景

  1)构建一个复杂对象,该复杂对象内部往往有许多部件(域)待初始化;

  2)通过不同的生成器,就可生成相应的复杂对象;

 

结构

  

  1)Product:即产品类。该类很复杂,它包含的域待初始化;

  2)Builder:抽象Builder类。它需要指定Product对象中各部件的生成方法:buildPart();

  3)ConcreteBuilder:Builder类的一个实现。它包含一个待初始化的Product对象,通过对其组件进行初始化,并提供一个成型对象的方法;

  4)Director:建造者。它包含一个抽象Builder对象,通过不同的Builder实现类,可生成不同的Product对象;在此过程中,也可选择性初始化部分组件,很灵活。

 

实例:

  Product有2个组件:component1和component2; Builder有2个实现类,生成含有不同组件组合的Product。

Product:

 1 package org.wgx.pattern.builder;
 2 
 3 public class Product {
 4     private String name = "";
 5     private String component1 = "";
 6     private String component2 = "";
 7     
 8     public Product(String name) {
 9         this.name = name;
10     }
11     
12     public void setName(String name) {
13         this.name = name;
14     }
15     
16     public void setComponent1(String c1) {
17         this.component1 = c1;
18     }
19     
20     public void setComponent2(String c2) {
21         this.component2 = c2;
22     }
23     
24     public String toString() {
25         String result = "This is Product: " + name + ", component1 is " + component1 + ", component2 is " + component2 + "\r\n";
26         return result;
27     }
28 }

 

Builder:

1 package org.wgx.pattern.builder;
2 
3 public interface Builder {
4     public void buildComponent1();
5     public void buildComponent2();
6     public Product getProduct();
7 }

 

ConcreteBuilderOne:

 1 package org.wgx.pattern.builder;
 2 
 3 public class ConcreteBuilderOne implements Builder {
 4     
 5     private Product product;
 6     
 7     public ConcreteBuilderOne() {
 8         product = new Product("first product");
 9     }
10     
11     @Override
12     public void buildComponent1() {
13         // TODO Auto-generated method stub
14         this.product.setComponent1("1a");
15     }
16 
17     @Override
18     public void buildComponent2() {
19         // TODO Auto-generated method stub
20         this.product.setComponent2("2a");
21     }
22 
23     @Override
24     public Product getProduct() {
25         // TODO Auto-generated method stub
26         return this.product;
27     }
28 
29 }

 

ConcreteBuilderTwo:

 1 package org.wgx.pattern.builder;
 2 
 3 public class ConcreteBuilderTwo implements Builder {
 4 
 5     private Product product;
 6     
 7     public ConcreteBuilderTwo() {
 8         product = new Product("second product");
 9     }
10         
11     @Override
12     public void buildComponent1() {
13         // TODO Auto-generated method stub
14         this.product.setComponent1("1b");
15     }
16 
17     @Override
18     public void buildComponent2() {
19         // TODO Auto-generated method stub
20         this.product.setComponent2("2b");
21     }
22 
23     @Override
24     public Product getProduct() {
25         // TODO Auto-generated method stub
26         return this.product;
27     }
28 
29 }

 

Director:

 1 package org.wgx.pattern.builder;
 2 
 3 public class Director {
 4     private Builder builder;
 5     
 6     public Director(Builder builder) {
 7         this.builder = builder;
 8     }
 9     
10     public Product constructProduct() {
11         builder.buildComponent1();
12         builder.buildComponent2();
13         return builder.getProduct();
14     }
15     
16 }

 

Client:

 1 package org.wgx.pattern.builder;
 2 
 3 public class Client {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         Builder builder1 = new ConcreteBuilderOne();
11         Director director1 = new Director(builder1);
12         System.out.println(director1.constructProduct());
13             
14         Builder builder2 = new ConcreteBuilderTwo();
15         Director director2 = new Director(builder2);
16         System.out.println(director2.constructProduct());
17     }
18 
19 }

 

结果:

This is Product: first product, component1 is 1a, component2 is 2a

This is Product: second product, component1 is 1b, component2 is 2b

 

 

 

posted @ 2013-05-18 16:59  Alex_Monkey  阅读(223)  评论(0编辑  收藏  举报