Java设计模式(3)建造者模式(Builder模式)

来源:互联网 发布:研究生发cscd难吗 知乎 编辑:程序博客网 时间:2024/05/19 18:44

Builder模式定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

Builder schema definition: the construction of a complex object from its separation, said that the same construction process can create different representations.


Builder模式是一步一步创建一个复杂的对象,它允许用户可以只通过指定复杂对象的类型和内容就可以构建它们。用户不知道内部的具体构建细节。Builder模式是非常类似抽象工厂模式,细微的区别大概只有在反复使用中才能体会到。

Builder model is  a complex object that created step by step,It allows the user can  build them only by specify the type of complex object and content.The user does not know internal build specific details.The Builder pattern is very similar to the abstract factory pattern,subtle differences probably can only experience in repeated use.

为何使用建造者模式

Why use the builder pattern

是为了将构建复杂对象的过程和它的部件解耦。注意:是解耦过程和部件。

For the process of building complex object and decoupling its process.
Note: is a decoupling process and components.

因为一个复杂的对象,不但有很多大量组成部分,如汽车,有很多部件:车轮、方向盘、发动机,还有各种小零件等等,部件很多,但远不止这些,如何将这些部件装配成一辆汽车,这个装配过程也很复杂(需要很好的组装技术),Builder模式就是为了将部件和组装过程分开。

A complex object ,Not only have a lot of a lot of parts, such as cars, there are a lot of parts: wheel, the steering wheel, engine, there are all sorts of small parts, etc., many parts, but much more than that, how will these parts assembled into a car, the assembly process is also very complex (need good assembly technique), the Builder pattern is to separate the parts and assembly process.

如何使用建造者模式

How to use the builder pattern:

首先假设一个复杂对象是由多个部件组成的,Builder模式是把复杂对象的创建和部件的创建分别开来,分别用Builder类和Director类来表示。

首先,需要一个接口,它定义如何创建复杂对象的各个部件:

First assumpt that  a complex object  compose multiple components, the Builder pattern separates the create complex objects and create components, respectively by Builder and Director.

First, you need an interface, which defines how to create the parts of a complex object:

public interface Builder { //创建部件A  比如创建汽车车轮 void buildPartA(); //创建部件B 比如创建汽车方向盘 void buildPartB(); //创建部件C 比如创建汽车发动机 void buildPartC(); //返回最后组装成品结果 (返回最后装配好的汽车) //成品的组装过程不在这里进行,而是转移到下面的Director类中进行. //从而实现了解耦过程和部件 Product getResult();}

With Director to build the complex object, and in the above interface Builder is encapsulated in how to create a small parts (complex object is made up of these parts), that is the content of the Director is how to final assembly components into a finished product:

用Director构建最后的复杂对象,而在上面Builder接口中封装的是如何创建一个个部件(复杂对象是由这些部件组成的),也就是说Director的内容是如何将部件最后组装成成品:

public class Director { private Builder builder; public Director( Builder builder ) {  this.builder = builder; } // 将部件partA partB partC最后组成复杂对象 //这里是将车轮 方向盘和发动机组装成汽车的过程 public void construct() {  builder.buildPartA();  builder.buildPartB();  builder.buildPartC(); }}

Builder的具体实现ConcreteBuilder:

  • 通过具体完成接口Builder来构建或装配产品的部件;
  • 定义并明确它所要创建的是什么具体东西;
  • 提供一个可以重新获取产品的接口。
Builder implement ConcreteBuilder:
Through concrete complete interface Builder to build and assembly of product components;
Define and identify what it is to create a specific things;
Provide a can regain some of the product interface.

public class ConcreteBuilder implements Builder { Part partA, partB, partC; public void buildPartA() {  //这里是具体如何构建partA的代码 }; public void buildPartB() {  //这里是具体如何构建partB的代码 }; public void buildPartC() {  //这里是具体如何构建partB的代码 }; public Product getResult() {  //返回最后组装成品结果 };}

复杂对象:产品Product: 

    public interface Product { }

复杂对象的部件: Parts of complicated objects:

    public interface Part { }

我们看看如何调用Builder模式:  How to call the Builder pattern:

ConcreteBuilder builder = new ConcreteBuilder();Director director = new Director( builder );director.construct();Product product = builder.getResult();

Builder模式的应用

在Java实际使用中,我们经常用到"池"(Pool)的概念,当资源提供者无法提供足够的资源,并且这些资源需要被很多用户反复共享时,就需要使用池。

"池"实际是一段内存,当池中有一些复杂的资源的"断肢"(比如数据库的连接池,也许有时一个连接会中断),如果循环再利用这些"断肢",将提高内存使用效率,提高池的性能。修改Builder模式中Director类使之能诊断"断肢"断在哪个部件上,再修复这个部件.

Builder model application

In actual use of the Java, we often use the concept of "Pool" (Pool), when resource provider cannot provide adequate resources, and these resources need to be shared among many users repeatedly, will need to use the Pool.

"Pool" is actually a memory, when there are some complex resource pool "limb" (such as a database connection pool, maybe sometimes a connection interruption), if these "limb" recycling, will improve the efficiency of memory usage, improve the performance of the pool.
Modify the Builder pattern class Director can make diagnosis "limb" broken on parts which fix the parts again

0 0
原创粉丝点击