Skip to content

Commit dc5111f

Browse files
committed
更新了开始和结束部分的几个段落
1 parent 101ca01 commit dc5111f

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

第二章-构造器模式.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,46 @@
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+
想象一下,我们要一个由多个部分合成的对象,而且合成需要一步步的来操作。这个对象在其自身所有部分都完全创建之前是不完整的。这就是构造器模式能够帮助我们的地方。构造器模式
7+
68
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>
79
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.
810

11+
实际的例子可以帮助我们理解什么是使用构造器的目的。假设我们想要创建一个HTML页面生成器,基本的HTML页面结构总是相同的:以<html>开始并以</html>结束;HTML区域的内部是<head>和</head>元素,
12+
913
The HTML page generation problem can be solved using the Builder pattern. In this pattern, there are two main participants: the builder and the director. The builder
1014
is responsible for creating the various parts of the complex object. In the HTML example, these parts are the title, heading, body, and the footer of the page. The director controls the building process using a builder instance. The HTML example means for calling the builder's functions for setting the title, the heading, and so on. Using a different builder instance allows us to create a different HTML page without touching any code of the director.
1115

12-
## A real-life example
16+
HTML页面生成器的问题可以使用构造器模式来解决。在这个模式中,
17+
18+
## 一个真实的例子
1319
The Builder design pattern is used in fast-food restaurants. The same procedure
1420
is always used to prepare a burger and the packaging (box and paper bag), even
1521
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
1622
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].
1723

24+
构造器模式被用在了快餐店。
25+
1826
图片:略
1927

20-
## A software example
28+
## 有关软件的例子
2129
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
2230
can be used for creating HTML pages with different layouts [j.mp/widgypb].
2331

2432
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].
2533

26-
## Use cases
34+
## 使用案例
2735
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].
2836

37+
当我们知道一个对象必须以多个步骤创建,
38+
2939
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
3040
at least two ways:
3141

3242
- With named parameters [j.mp/sobuipython]
43+
- 使用有名字的参数
3344
- With argument list unpacking [j.mp/arglistpy]
45+
- 参数列表解包
3446

3547
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.
3648

@@ -67,12 +79,14 @@ if __name__ == '__main__':
6779
print(mac_mini)
6880
```
6981

70-
>#### Note
82+
>#### 注释
7183
>Notice the nested *MacMini14 class*. This is a neat way of forbidding the direct instantiation of a class.
7284
7385
Another option is buying a custom PC. In this case, you use the Builder pattern. You are the director that gives orders to the manufacturer (builder) about
7486
your ideal computer specifications. Code-wise, this looks like the following (*computer-builder.py*):
7587

88+
另外一个选择买一台定制的PC。在这个例子中,你可以使用构造器模式。
89+
7690
```python
7791
class Computer:
7892
def __init__(self, serial_number):
@@ -498,19 +512,26 @@ class Pizza:
498512

499513
Adapt the pizza example to make use of the Fluent Builder pattern. Which version of the two do you prefer? What are the pros and cons of each version?
500514

501-
## Summary
515+
## 总结
502516
In this chapter, we have seen how to use the Builder design pattern. We use the Builder pattern for creating an object in situations where using the Factory pattern (either a Factory Method or an Abstract Factory) is not a good option. A Builder pattern is usually a better candidate than a Factory pattern when:
503517

504518
- We want to create a complex object (an object composed of many parts and created in different steps that might need to follow a specific order).
519+
- 我们想创建一个复杂的对象(对象由很多部分合成,而且按照不同的步骤创建,这就需要遵循一个特定的顺序)。
505520

506521
- Different representations of an object are required, and we want to keep the construction of an object decoupled from its representation
507522

508523
- We want to create an object at one point in time but access it at a later point
509524

510525
We saw how the Builder pattern is used in fast-food restaurants for preparing meals, and how two third-party Django packages, django-widgy and django-query- builder, use it for generating HTML pages and dynamic SQL queries, respectively. We focused on the differences between a Builder pattern and a Factory pattern, and gave a preconfigured (Factory) versus customer (Builder) computer order analogy to clarify them.
511526

527+
我们见过了在快餐店如何使用构造器来为备餐服务,以及两个第三方店Django,django-widgy 和 django-query- builder如何分别地被用来生成HTML页面和动态的SQL查询。我们关注了构造器模式和工厂模式之间的区别,
528+
512529
In the implementation part, we have seen how to create a pizza ordering application, which has preparation dependencies. There are many recommended interesting exercises in this chapter, including implementing a Fluent Builder.
513530

531+
在实现部分,我们见到了如何创建一个披萨订餐应用,这个应用有一些前置以来。在这一章,推荐了很多有趣的联系,其中就包括实现一个流体构造器。
532+
514533
In the next chapter, you will learn about the last creational design pattern covered in this book: the Prototype pattern, which is used for cloning an object.
515534

535+
在下一章,你会学习到
536+
516537

0 commit comments

Comments
 (0)