Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1744754
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-02-07 10:25:40

The intent of the builder Pattern is to separate the construction of complex object from its representation,
so that the same construction process can create different representation. Adding a new implementation 
(i.e adding a new builder) becomes easier, the object construction process becomes indepedent of the components that make up the object. This provides more control over the object construction process.
照书上的抄一段,Builder pattern 的目的是为了将一个复杂的对象的构造和表现分开, 同样的构造过程可以
生成不同的表现。 增加新的实现如增加一个builder 很容易,对象构造过程和对象的组成都是独立的,
对于对象构造能有更佳控制。

Builder: 
Specifies an abstract interface for creating parts of a product object. 
ConcreteBuilder:
Construct and assembles part of the product by implementing the Builder interface. 
Defines and keeps track of the representation it creates.
Provides an interface for retrieving the product.
Director:
Constructs an object using the Builder interface 
Product: 
Represents the complex object under construction , ConcreteBuilder builds the product's
internal representation and defines the process by which it's assembled.
Include classes that define the constituent parts, including interfaces for assembling the parts into the final result. 

代码:

点击(此处)折叠或打开

  1. package builderpattern;

  2. import java.util.LinkedList;

  3. interface IBuilder{
  4.     void BuildBody();
  5.     void InsertWheels();
  6.     void AddHeadlights();
  7.     Product GetVehicle();
  8. }

  9. //Car is ConcreteBuilder
  10. class Car implements IBuilder{

  11.     private Product product = new Product();

  12.     @Override
  13.     public void BuildBody(){
  14.         product.Add("This is a body of a Car");
  15.     }

  16.     @Override
  17.     public void InsertWheels(){
  18.         product.Add("4 wheels are added");
  19.     }

  20.     @Override
  21.     public void AddHeadlights(){
  22.         product.Add("2 Headlights are added");
  23.     }

  24.     @Override
  25.     public Product GetVehicle(){
  26.         return product;
  27.     }
  28. }
  29. //Motycycle is a ConcreteBuilder
  30. class MotorCycle implements IBuilder{
  31.     private Product product = new Product();

  32.     @Override
  33.     public void BuildBody(){
  34.         product.Add("This is a body of a MotorCycle");
  35.     }

  36.     @Override
  37.     public void InsertWheels(){
  38.         product.Add("2 wheels are added");
  39.     }

  40.     @Override
  41.     public void AddHeadlights(){
  42.         product.Add("1 Headlights are added");
  43.     }

  44.     @Override
  45.     public Product GetVehicle(){
  46.         return product;
  47.     }
  48. }

  49. //Product
  50. class Product{
  51.     //We can use any data structure here, used LinkedList
  52.     private LinkedList<String> parts;

  53.     public Product(){
  54.         parts = new LinkedList<String> ();
  55.     }

  56.     public void Add(String part){
  57.         //Adding parts
  58.         parts.addLast(part);
  59.     }

  60.     public void Show(){
  61.         System.out.println("\n Proudct completed as below:");
  62.         for(int i=0;i<parts.size();i++){
  63.             System.out.println(parts.get(i));
  64.         }
  65.     }
  66. }

  67. //Director
  68. class Director{
  69.     IBuilder myBuilder;
  70.     //A series of steps-for the production
  71.     public void Construct(IBuilder builder){
  72.         myBuilder = builder;
  73.         myBuilder.BuildBody();
  74.         myBuilder.InsertWheels();
  75.         myBuilder.AddHeadlights();
  76.     }
  77. }

  78. class BuilderPattern{

  79.     public static void main(String[] args){
  80.         System.out.println("*** Builder Pattern Demo***");
  81.         Director director = new Director();

  82.         IBuilder carBuilder = new Car();
  83.         IBuilder motorBuilder = new MotorCycle();

  84.         //Makeing car
  85.         director.Construct(carBuilder);
  86.         Product p1 = carBuilder.GetVehicle();
  87.         p1.Show();

  88.         //Making motorcycle
  89.         director.Construct(motorBuilder);
  90.         Product p2 = motorBuilder.GetVehicle();
  91.         p2.Show();

  92.     }
  93. }

另外一种形式的Builder pattern. 

点击(此处)折叠或打开

  1. package builderpattern;

  2. class Cake{
  3.     private final double sugar;
  4.     private final double butter;
  5.     private final double milk;
  6.     private final int cherry;

  7.     public static class CakeBuilder{
  8.         private double sugar;
  9.         private double butter;
  10.         private double milk;
  11.         private int cherry;

  12.         //Builder methods for setting property
  13.         public CakeBuilder sugar(double cup){
  14.             this.sugar = cup;
  15.             return this;
  16.         }

  17.         public CakeBuilder butter(double cup){
  18.             this.butter = cup;
  19.             return this;
  20.         }

  21.         public CakeBuilder milk(double cup){
  22.             this.milk = cup;
  23.             return this;
  24.         }

  25.         public CakeBuilder cherry(int number){
  26.             this.cherry = number;
  27.             return this;
  28.         }

  29.         //return fully build object
  30.         public Cake build(){
  31.             return new Cake(this);
  32.         }

  33.     }

  34.     //private constructor to enforce object creation through builder
  35.     private Cake(CakeBuilder builder){
  36.         this.sugar = builder.sugar;
  37.         this.butter = builder.butter;
  38.         this.milk = builder.milk;
  39.         this.cherry = builder.cherry;
  40.     }

  41.     @Override
  42.     public String toString(){
  43.         return "Cake [ sugar="+ sugar +", butter="+butter +",milk="+milk+",cherry="+cherry
  44.             +"]";
  45.     }
  46. }


  47. public class BuildPatternDemo {
  48.     public static void main(String[] args){
  49.         //Creating object using Builder pattern in java
  50.         Cake WhiteCake= new Cake.CakeBuilder().sugar(1).butter(0.5).milk(0.5).cherry(10).build();
  51.         System.out.println(WhiteCake);

  52.         Cake DietCake = new Cake.CakeBuilder().build();
  53.         System.out.println(DietCake);
  54.     }
  55. }


阅读(445) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~