Skip to content

Commit cd944f0

Browse files
committed
更新两个段落
1 parent dc5111f commit cd944f0

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

第二章-构造器模式.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Imagine that we want to create an object that is composed of multiple parts and the composition needs to be done step by step. The object is not complete unless all its parts are fully created. That's where the Builder design pattern can help us. The Builder pattern separates the construction of a complex object from its representation. By keeping the construction separate from the representation, the same construction can be used to create several different representations [GOF95, page 110], [j.mp/builderpat].
55

6-
想象一下,我们要一个由多个部分合成的对象,而且合成需要一步步的来操作。这个对象在其自身所有部分都完全创建之前是不完整的。这就是构造器模式能够帮助我们的地方。构造器模式
6+
想象一下,我们要一个由多个部分合成的对象,而且合成需要一步步的来操作。这个对象在其自身所有部分都完全创建之前是不完整的。这就是构造器模式能够帮助我们的地方。构造器模式通过自己的表现将一个复杂对象的构造进行分离。通过使用表现的构造分离,相同的构造介意被用到多个不同到表现上。
77

88
A practical example can help us understand what the purpose of the Builder pattern is. Suppose that we want to create an HTML page generator, the basic structure (construction part) of an HTML page is always the same: it begins with <html>
99
and finishes with </html>; inside the HTML section are the <head> and </head> elements, inside the head section are the <title> and </title> elements, and so forth. But the representation of the page can differ. Each page has its own title, its own headings, and different <body> contents. Moreover, the page is usually built in steps: one function adds the title, another adds the main heading, another the footer, and so on. Only after the whole structure of a page is complete can it be shown to the client using a final render function. We can take it even further and extend the HTML generator so that it can generate totally different HTML pages. One page might contain tables, another page might contain image galleries, yet another page contains the contact form, and so on.
@@ -16,32 +16,34 @@ is responsible for creating the various parts of the complex object. In the HTML
1616
HTML页面生成器的问题可以使用构造器模式来解决。在这个模式中,
1717

1818
## 一个真实的例子
19-
The Builder design pattern is used in fast-food restaurants. The same procedure
20-
is always used to prepare a burger and the packaging (box and paper bag), even
21-
if there are many different kinds of burgers (classic, cheeseburger, and more) and different packages (small-sized box, medium-sized box, and so forth). The difference between a classic burger and a cheeseburger is in the representation, and not in the construction procedure. The director is the cashier who gives instructions about what needs to be prepared to the crew, and the builder is the person from the
19+
The Builder design pattern is used in fast-food restaurants. The same procedure is always used to prepare a burger and the packaging (box and paper bag), even if there are many different kinds of burgers (classic, cheeseburger, and more) and different packages (small-sized box, medium-sized box, and so forth). The difference between a classic burger and a cheeseburger is in the representation, and not in the construction procedure. The director is the cashier who gives instructions about what needs to be prepared to the crew, and the builder is the person from the
2220
crew that takes care of the specific order. The following figure provided by *www. sourcemaking.com* shows a **Unified Modeling Language (UML)** sequence diagram of the communication that takes place between the customer (client), the cashier (director), and the crew (builder) when a kid's menu is ordered [j.mp/builderpat].
2321

24-
构造器模式被用在了快餐店。
22+
构造器模式被用在了快餐店。相同的步骤一直用在了准备汉堡和打包上,即使存在不同类型的汉堡(原味,起司,等等)以及不同的包装(小号的盒子,中号的盒子,等等)原味汉堡和起司汉堡之间不同在于表现上,而不是在构造过程上。
2523

2624
图片:略
2725

28-
## 有关软件的例子
29-
The HTML example that was mentioned at the beginning of the chapter is actually used by **django-widgy**, a third-party tree editor for Django that can be used as a **Content Management System (CMS)**. The django-widgy editor contains a page builder that
30-
can be used for creating HTML pages with different layouts [j.mp/widgypb].
26+
## 与软件相关的例子
27+
The HTML example that was mentioned at the beginning of the chapter is actually used by **django-widgy**, a third-party tree editor for Django that can be used as a **Content Management System (CMS)**. The django-widgy editor contains a page builder that can be used for creating HTML pages with different layouts [j.mp/widgypb].
28+
29+
本章开始提到的HTML例子实际上是使用的是django-widgy,这是一个第三方的Django树形编辑器包,它能够当作CMS来使用。django-widgy包含了一个构造器,它能够用来创建不同布局的HTML页面。
3130

3231
The **django-query-builder** library is another third-party Django library that relies on the Builder pattern. The django-query-builder library can be used for building SQL queries dynamically. Using this, we can control all aspects of a query and create a different range of queries, from simple to very complex [j.mp/djangowidgy].
3332

33+
django-query-builder库是另外一个第三方的Django库,它依赖于构造器模式。
34+
3435
## 使用案例
3536
We use the Builder pattern when we know that an object must be created in multiple steps, and different representations of the same construction are required. These requirements exist in many applications such as page generators (like the HTML page generator mentioned in this chapter), document converters [GOF95, page 110], and User Interface (UI) form creators [j.mp/pipbuild].
3637

37-
当我们知道一个对象必须以多个步骤创建,
38+
在我们知道对象必须用到多个步骤才能创建,以及需要相同构造的不同表现时才会使用构造器模式。这些需求存在于很多的应用中,比如页面生成器(类似于本章提到的页面生成器),文档转换器,以及用户界面(UI)的表单创建器。
3839

3940
Some resources mention that the Builder pattern can also be used as a solution to the telescopic constructor problem [j.mp/wikibuilder]. The telescopic constructor problem occurs when we are forced to create a new constructor for supporting different ways of creating an object. The problem is that we end up with many constructors and long parameter lists, which are hard to manage. An example of the telescopic constructor is listed at the stackoverflow website [j.mp/sobuilder]. Fortunately, this problem does not exist in Python, because it can be solved in
4041
at least two ways:
4142

4243
- With named parameters [j.mp/sobuipython]
43-
- 使用有名字的参数
4444
- With argument list unpacking [j.mp/arglistpy]
45+
46+
- 使用有名字的参数
4547
- 参数列表解包
4648

4749
At this point, the distinction between the Builder pattern and the Factory pattern might not be very clear. The main difference is that a Factory pattern creates an object in a single step, whereas a Builder pattern creates an object in multiple steps, and almost always through the use of a director. Some targeted implementations of the Builder pattern like Java's **StringBuilder** bypass the use of a director, but that's the exception to the rule.
@@ -51,6 +53,8 @@ to return the final object when it needs it [GOF95, page 113], [j.mp/builderpat]
5153

5254
The new computer analogy might help to distinguish between a Builder pattern and a Factory pattern. Assume that you want to buy a new computer. If you decide to buy a specific preconfigured computer model, for example, the latest Apple 1.4 GHz Mac mini, you use the Factory pattern. All the hardware specifications are already predefined by the manufacturer, who knows what to do without consulting you. The manufacturer typically receives just a single instruction. Code-wise, this would look like the following (*apple-factory.py*):
5355

56+
新计算机的分析有助于区别构造器模式和工厂模式。假设你需要买一台新电脑。如果你决定买预先配置好的电脑,例如最新的Apple 1.4Ghz Mac mini,你使用的是工厂模式。所有硬件规格都已经由厂商预定义了,
57+
5458
```python
5559
MINI14 = '1.4GHz Mac mini'
5660

0 commit comments

Comments
 (0)