Skip to content

Commit 33d74e9

Browse files
author
ictar
committed
add translation for 复合构建器模式(Composite Builder Pattern),一个声明式编程的例子
1 parent ee26b46 commit 33d74e9

2 files changed

Lines changed: 32 additions & 27 deletions

File tree

‎Others/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@
5151
- [如何使用Python和Pandas处理大量的JSON数据集](./如何使用Python和Pandas处理大量的JSON数据集.md)
5252

5353
如何处理大量的(指无法一次性存入内存)JSON数据集呢?本文给出了一个第三方库ijson来迭代的处理此问题。同时,通过一个简单的交通管制数据的分析,向我们娓娓道来如何使用Pandas处理分析可视化数据……
54+
55+
- [复合构建器模式(Composite Builder Pattern),一个声明式编程的例子](复合构建器模式(Composite Builder Pattern),一个声明式编程的例子)
56+
57+
针对于那些属性来自于多个源的复杂对象来说,本文给出了一个例子,提出复合构建器模式以方便编程。

‎Others/复合构建器模式(Composite Builder Pattern),一个声明式编程的例子.md‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
---
44

5-
I'm calling this the **Composite Builder** pattern. This may have other names, but I haven't seen them. It could simply be lack of research into prior art. I suspect this isn't very new. But I thought it was cool way to do some declarative Python programming.
5+
我称其为**复合构建器(Composite Builder)**模式。它可能还有其他名字,但目前我尚未见到。有可能只是因为缺乏对现有技术的研究。我怀疑,这并不是很新的模式。但我觉得,这是进行一些声明式Python编程的很酷的方法。
66

7-
Here's the concept.
7+
下面是概念。
88

99
```py
1010
class TheCompositeThing(Builder):
@@ -13,17 +13,17 @@ class TheCompositeThing(Builder):
1313
more_attributes = MoreItems("more args")
1414
```
1515

16-
The idea is that when we create an instance of **TheCompositeThing**, we get a complex object, built from various data sources. We want to use this in the following kind of context:
16+
它的思想在于,当我们创建**TheCompositeThing**的一个实例时,我们得到了一个复杂对象,这个对象是根据各种数据源进行构建的。我们想在下面这一类上下文中使用它:
1717

1818
```py
1919
with some_config_path.open() as config:
2020
the_thing = TheCompositeThing().substitute(config)
2121
print(json.dumps(the_thing))
2222
```
2323

24-
We want to open some configuration file -- something that's unique to an environment -- and populate the complex object in one smooth motion. Once we have the complex object, it can then be used in some way, perhaps serialized as a JSON or YAML document.
24+
我们想打开一些配置文件 —— 一些对环境唯一的文件 —— 然后以一种平滑的方式填充该复杂对象。一旦获得了这个复杂对象,它就可以以一些方式为我们所用,例如作为JSON或者YAML文档被序列化。
2525

26-
Each **Item** has a **get()** method that accepts the configuration as input. These do some computation to return a useful result. In some cases, the computation is kind of degenerate case:
26+
每一个**Item**都有一个**get()**方法,该方法接受配置所为其输入。它们进行一些计算,然后返回一个有用的结果。在某些情况下,这些计算有点落后:
2727

2828
```py
2929
class LiteralItem(Item):
@@ -33,15 +33,15 @@ class LiteralItem(Item):
3333
return self.value
3434
```
3535

36-
This shows how we jam a literal value into the output. Other values might involve elaborate computations, or lookups in the configuration, or a combination of the two.
36+
上面显示了我们如何将一个文本值赋给输出。其他值可能涉及复杂的计算,或者配置查找,或者两者的结合。
3737

38-
## Why Use a Declarative Style?
38+
## 为什么要使用一种声明式风格?
3939

40-
This declarative style can be handy when each of the Items in **TheCompositeThing** involves rather complex, but completely independent computations. There's no dependency here, so the **substitute()** method can fill in the attributes in any order. Or -- perhaps -- not fill the attributes until they're actually requested. This pattern allows eager or lazy calculation of the attributes.
40+
当**TheCompositeThing**中的每个项都涉及到想到复杂,但完全独立的计算时,这种声明式风格可能很方便。因为不存在依赖性,所以**substitute()**方法可以以任何顺序为属性赋值。或者,可能,不赋值,直到真正需要它们的时候。这种模式即允许立即计算出属性值,又允许属性的即时计算。
4141

42-
This pattern applies to building complex [AWS Cloud Formation Templates](https://aws.amazon.com/cloudformation/aws-cloudformation-templates/) as an example. We often need to make a global tweak to a large number of templates so that we can rebuild a server farm. There's little or no dependency among the **Item** values being filled in. There's no strange "ripple effect" of a change in one place also showing up in another place because of an obscure dependency between items.
42+
作为一个例子,这种模式适用于构建复杂的[AWS云形成模板](https://aws.amazon.com/cloudformation/aws-cloudformation-templates/)。我们通常需要对大量的模板进行一个全局调整,以便于重建一个服务器场。而被填充的**Item**之间很少或没有依赖关系。由于项之间的模糊依赖关系,并不存在这种在一个地方进行的修改同时也出现在另一个地方的奇怪的“涟漪效应”。
4343

44-
We can extend this to have a kind of pipeline with each stage created in a declarative style. In this more complex situation, we'll have several tiers of **Items** that fill in the composite object. The first-stage **Items** depend on one source. The second stage **Items** depend on the first-stage **Items**.
44+
我们可以对其进行扩展,使得在声明式风格中创建的每个阶段都带有一个管道。在这种更复杂的情况下,填充到该复杂对象的**Items**将具有多层次。第一层次的**Items**依赖于一个源。第二层次的**Items**依赖于第一层次的**Items**.
4545

4646
```py
4747
class Stage1(Builder):
@@ -53,9 +53,9 @@ class Stage2(Builder):
5353
item_b = Stage_2_Another(355, 113)
5454
```
5555

56-
We can then create a **Stage1** object from external configuration or inputs. We can create the derived **Stage2** object from the **Stage1** object.
56+
然后,我们可以根据外部配置或输入创建一个**Stage1**对象。接着可以创建从该**Stage1**对象派生的**Stage2**对象。
5757

58-
And yes. This seems like useless metaprogramming. We could -- more simply -- do something like this::
58+
是滴。这看起来项无用的元编程。我们可以(更简单)的这样做::
5959

6060
```py
6161
class Stage2:
@@ -64,9 +64,9 @@ class Stage2:
6464
self.item_b = Stage_2_Another(355, 113, stage_1, config)
6565
```
6666

67-
We've eagerly computed the attributes during `__init__()` processing.
67+
我们已经急切地在`__init__()`处理阶段计算出了属性值。
6868

69-
Or perhaps this::
69+
或者,也许是这样::
7070

7171
```py
7272
class Stage2:
@@ -81,26 +81,27 @@ class Stage2:
8181
return Stage_2_Another(355, 113, self.stage_1, self.config)
8282
```
8383

84-
Here we've been lazy and only computed attribute values as they are requested.
84+
这里,我们启动即时模式,也就是说,只有在请求属性的时候,才计算属性值。
8585

86-
## Benefits
86+
## 好处
8787

88-
We've looked at three ways to build composite objects:
88+
我们已经看到了三种构建复杂对象的方法:
8989

90-
1. As independent attributes with an flexible but terse implementation.
91-
2. As attributes during **__init__() **using sequential code that doesn't assure independence.
92-
3. As properties using wordy code.
93-
What's the value proposition? Why is this declarative technique interesting?
90+
1. 作为独立属性,它具有一种灵活但是简洁的实现。
91+
2. 作为在**__init__() **中使用了顺序代码的属性,它们不能保证独立性。
92+
3. 作为使用冗余代码的属性。
9493

95-
I find that the the **Declarative Builder** pattern is handy because it gives me the following benefits.
94+
哪个是倡导的方式呢?为什么这种声明式技术有趣?
9695

97-
* The attributes **must** be built independently. We can -- without a second thought -- rearrange the attributes and not worry about one calculation interfering with another attribute.
98-
* The attributes can be built eagerly or lazily. Details don't matter. We don't expose the implementation details via **__init__** or **@property**.
99-
* The class definition becomes a configuration item. A support technician without deep Python knowledge can edit the definition of **TheCompositeThing** successfully.
96+
我发现**声明式构建器(Declarative Builder)**模式是很方便的,因为它给我带来了以下好处。
10097

101-
I think this kind of lazy, declarative programming is useful for some applications. It's ideal in those cases where we need to isolate a number of computations from each other to allow the software to evolve without breaking.
98+
* 属性**必须**被独立构建。我们可以(毫不犹豫地)重新排列这些属性,而不用担心计算会干扰到另一个属性。
99+
* 属性可以立即构建或者即时构建。细节并不重要。我们并不通过**__init__**或者**@property**来公开实现细节。
100+
* 类定义变成了一个配置项。技术支持人员不需要具备Python深层支持就可以成功的编辑**TheCompositeThing**的定义。
102101

102+
我认为对于一些应用而言,这种即时的声明式编程是很有用的。它在一些情况
103+
下表现完美,例如,当我们需要彼此隔离大量的计算,从而允许软件在不被破坏的情况进行演变的时候。
103104

104105

105106

106-
It may be a stretch, but I think this shows the [Depedency Inversion Principle](https://en.wikipedia.org/wiki/Dependency_inversion_principle). To an extent, we've moved all of the dependencies to the visible list of attributes within these classes. The items classes do not depend on each other; they depend on configuration or perhaps previous stage composite objects. Since there are no methods involved in the class defintion, we can change the class freely. Each subclass of **Builder** is more like a configuration item than it is like code. In Python, particularly, we can change the class freely without the agony of a rebuild.
107+
这可能是一个延伸,但我认为它显示了[依赖倒置原则](https://en.wikipedia.org/wiki/Dependency_inversion_principle)。在某种程度上,我们已经将所有依赖移到这些类中属性的可见列表。这些项类不互相依赖;它们依赖于配置,或者可能是前一个接单的复合对象。由于在类定义中不包含任何方法,因此我们可以任意修改类。比起代码,每一个**Builder**子类更像一个配置项。特别是在Python中,我们可以任意修改类,而不用管重建的痛苦。

0 commit comments

Comments
 (0)