You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 33d74e9
Browse filesBrowse the repository at this point in the historyBrowse files
ictar
committed
add translation for 复合构建器模式(Composite Builder Pattern),一个声明式编程的例子
Copy file name to clipboardExpand all lines: Others/复合构建器模式(Composite Builder Pattern),一个声明式编程的例子.md
+28-27Lines changed: 28 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@
2
2
3
3
---
4
4
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.
@@ -13,17 +13,17 @@ class TheCompositeThing(Builder):
13
13
more_attributes = MoreItems("more args")
14
14
```
15
15
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:
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.
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:
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
+
上面显示了我们如何将一个文本值赋给输出。其他值可能涉及复杂的计算,或者配置查找,或者两者的结合。
37
37
38
-
## Why Use a Declarative Style?
38
+
## 为什么要使用一种声明式风格?
39
39
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.
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.
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**.
Here we've been lazy and only computed attribute values as they are requested.
84
+
这里,我们启动即时模式,也就是说,只有在请求属性的时候,才计算属性值。
85
85
86
-
## Benefits
86
+
## 好处
87
87
88
-
We've looked at three ways to build composite objects:
88
+
我们已经看到了三种构建复杂对象的方法:
89
89
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. 作为使用冗余代码的属性。
94
93
95
-
I find that the the **Declarative Builder** pattern is handy because it gives me the following benefits.
94
+
哪个是倡导的方式呢?为什么这种声明式技术有趣?
96
95
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.
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.
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.
0 commit comments