Skip to content

Commit 0f966a2

Browse files
authored
translation: Added Chinese Translation for Combinator and Monitor patterns (#2703)
* Create combinator * translation for combinator and monitor to Chinese
1 parent 83876e1 commit 0f966a2

File tree

4 files changed

+302
-0
lines changed

4 files changed

+302
-0
lines changed

localization/zh/combinator/README.md

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
---
2+
title: Combinator
3+
category: Idiom
4+
language: zh
5+
tag:
6+
- Reactive
7+
---
8+
9+
## 或称
10+
11+
构图模式
12+
13+
## 目的
14+
15+
功能模式代表了一种以组合功能为中心的图书馆组织风格。
16+
简单地说,有一些类型 T,一些用于构造类型 T 的“原始”值的函数,以及一些可以以各种方式组合类型 T 的值以构建更复杂的类型 T 值的“组合器”。
17+
18+
## 解释
19+
20+
真实世界例子
21+
22+
> 在计算机科学中,组合逻辑被用作计算的简化模型,用于可计算性理论和证明理论。 尽管组合逻辑很简单,但它捕获了计算的许多基本特征。
23+
>
24+
25+
通俗的说
26+
> 组合器允许从先前定义的“事物”创建新的“事物”。
27+
>
28+
29+
维基百科说
30+
31+
> 组合器是一个高阶函数,仅使用函数应用程序和之前定义的组合器来定义其参数的结果。
32+
>
33+
34+
**程序示例**
35+
36+
翻译上面的组合器示例。 首先,我们有一个由几个方法`contains`, `not`, `or`, `and`组成的接口。
37+
38+
```java
39+
// 用于查找文本中的行的功能界面。
40+
public interface Finder {
41+
42+
// 在文本中查找行的函数。
43+
List<String> find(String text);
44+
45+
// 函数{@link #find(String)}的简单实现。
46+
static Finder contains(String word) {
47+
return txt -> Stream.of(txt.split("\n"))
48+
.filter(line -> line.toLowerCase().contains(word.toLowerCase()))
49+
.collect(Collectors.toList());
50+
}
51+
52+
// 组合器:not。
53+
default Finder not(Finder notFinder) {
54+
return txt -> {
55+
List<String> res = this.find(txt);
56+
res.removeAll(notFinder.find(txt));
57+
return res;
58+
};
59+
}
60+
61+
// 组合器:or。
62+
default Finder or(Finder orFinder) {
63+
return txt -> {
64+
List<String> res = this.find(txt);
65+
res.addAll(orFinder.find(txt));
66+
return res;
67+
};
68+
}
69+
70+
// 组合器:and。
71+
default Finder and(Finder andFinder) {
72+
return
73+
txt -> this
74+
.find(txt)
75+
.stream()
76+
.flatMap(line -> andFinder.find(line).stream())
77+
.collect(Collectors.toList());
78+
}
79+
...
80+
}
81+
```
82+
83+
然后我们还有另一个组合器用于一些复杂的查找器`advancedFinder`, `filteredFinder`, `specializedFinder``expandedFinder`
84+
85+
```java
86+
// 由简单取景器组成的复杂取景器。
87+
public class Finders {
88+
89+
private Finders() {
90+
}
91+
92+
// Finder 用于查找复杂的查询。
93+
public static Finder advancedFinder(String query, String orQuery, String notQuery) {
94+
return
95+
Finder.contains(query)
96+
.or(Finder.contains(orQuery))
97+
.not(Finder.contains(notQuery));
98+
}
99+
100+
// 过滤查找器也会查找包含排除查询的查询。
101+
public static Finder filteredFinder(String query, String... excludeQueries) {
102+
var finder = Finder.contains(query);
103+
104+
for (String q : excludeQueries) {
105+
finder = finder.not(Finder.contains(q));
106+
}
107+
return finder;
108+
}
109+
110+
// 专门查询。 每个下一个查询都会在上一个结果中查找。
111+
public static Finder specializedFinder(String... queries) {
112+
var finder = identMult();
113+
114+
for (String query : queries) {
115+
finder = finder.and(Finder.contains(query));
116+
}
117+
return finder;
118+
}
119+
120+
// 扩展查询。 寻找替代品。
121+
public static Finder expandedFinder(String... queries) {
122+
var finder = identSum();
123+
124+
for (String query : queries) {
125+
finder = finder.or(Finder.contains(query));
126+
}
127+
return finder;
128+
}
129+
...
130+
}
131+
```
132+
133+
现在我们已经创建了组合器的接口和方法。 现在我们有一个处理这些组合器的应用程序。
134+
135+
```java
136+
var queriesOr = new String[]{"many", "Annabel"};
137+
var finder = Finders.expandedFinder(queriesOr);
138+
var res = finder.find(text());
139+
LOGGER.info("the result of expanded(or) query[{}] is {}", queriesOr, res);
140+
141+
var queriesAnd = new String[]{"Annabel", "my"};
142+
finder = Finders.specializedFinder(queriesAnd);
143+
res = finder.find(text());
144+
LOGGER.info("the result of specialized(and) query[{}] is {}", queriesAnd, res);
145+
146+
finder = Finders.advancedFinder("it was", "kingdom", "sea");
147+
res = finder.find(text());
148+
LOGGER.info("the result of advanced query is {}", res);
149+
150+
res = Finders.filteredFinder(" was ", "many", "child").find(text());
151+
LOGGER.info("the result of filtered query is {}", res);
152+
153+
private static String text() {
154+
return
155+
"It was many and many a year ago,\n"
156+
+ "In a kingdom by the sea,\n"
157+
+ "That a maiden there lived whom you may know\n"
158+
+ "By the name of ANNABEL LEE;\n"
159+
+ "And this maiden she lived with no other thought\n"
160+
+ "Than to love and be loved by me.\n"
161+
+ "I was a child and she was a child,\n"
162+
+ "In this kingdom by the sea;\n"
163+
+ "But we loved with a love that was more than love-\n"
164+
+ "I and my Annabel Lee;\n"
165+
+ "With a love that the winged seraphs of heaven\n"
166+
+ "Coveted her and me.";
167+
}
168+
```
169+
170+
**程序输出:**
171+
172+
```java
173+
the result of expanded(or) query[[many, Annabel]] is [It was many and many a year ago,, By the name of ANNABEL LEE;, I and my Annabel Lee;]
174+
the result of specialized(and) query[[Annabel, my]] is [I and my Annabel Lee;]
175+
the result of advanced query is [It was many and many a year ago,]
176+
the result of filtered query is [But we loved with a love that was more than love-]
177+
```
178+
179+
现在我们可以设计我们的应用程序,使其具有查询查找功能`expandedFinder`, `specializedFinder`, `advancedFinder`, `filteredFinder`,这些功能均派生自`contains`, `or`, `not`, `and`
180+
181+
182+
## 类图
183+
![alt text](./etc/combinator.urm.png "Combinator class diagram")
184+
185+
## 适用性
186+
在以下情况下使用组合器模式:
187+
188+
- 您可以从更简单的值创建更复杂的值,但具有相同的类型(它们的组合)
189+
190+
## 好处
191+
192+
- 从开发人员的角度来看,API 由领域中的术语组成。
193+
- 组合阶段和应用阶段之间有明显的区别。
194+
- 首先构造一个实例,然后执行它。
195+
- 这使得该模式适用于并行环境。
196+
197+
198+
## 现实世界的例子
199+
200+
- java.util.function.Function#compose
201+
- java.util.function.Function#andThen
202+
203+
## 鸣谢
204+
205+
- [Example for java](https://gtrefs.github.io/code/combinator-pattern/)
206+
- [Combinator pattern](https://wiki.haskell.org/Combinator_pattern)
207+
- [Combinatory logic](https://wiki.haskell.org/Combinatory_logic)
Loading

localization/zh/monitor/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: Monitor
3+
category: Concurrency
4+
language: zh
5+
tag:
6+
- Performance
7+
---
8+
9+
## 或称
10+
11+
监控对象模式
12+
13+
## 目的
14+
15+
主要目的是为多个线程或进程提供一种结构化和受控的方式来安全地访问和操作共享资源,例如变量、数据结构或代码的关键部分,而不会导致冲突或竞争条件。
16+
17+
## 解释
18+
19+
通俗的说
20+
21+
> 监视器模式用于强制对数据进行单线程访问。 一次只允许一个线程在监视器对象内执行代码。
22+
23+
维基百科说
24+
25+
> 在并发编程(也称为并行编程)中,监视器是一种同步构造,它允许线程具有互斥性和等待(阻止)特定条件变为假的能力。 监视器还具有向其他线程发出信号通知其条件已满足的机制。
26+
27+
**程序示例**
28+
29+
考虑有一家银行通过转账方式将钱从一个帐户转移到另一个帐户。 它是`同步`意味着只有一个线程可以访问此方法,因为如果许多线程访问它并在同一时间将资金从一个帐户转移到另一个帐户,则余额会发生变化!
30+
31+
```
32+
class Bank {
33+
34+
private int[] accounts;
35+
Logger logger;
36+
37+
public Bank(int accountNum, int baseAmount, Logger logger) {
38+
this.logger = logger;
39+
accounts = new int[accountNum];
40+
Arrays.fill(accounts, baseAmount);
41+
}
42+
43+
public synchronized void transfer(int accountA, int accountB, int amount) {
44+
if (accounts[accountA] >= amount) {
45+
accounts[accountB] += amount;
46+
accounts[accountA] -= amount;
47+
logger.info("Transferred from account :" + accountA + " to account :" + accountB + " , amount :" + amount + " . balance :" + getBalance());
48+
}
49+
}
50+
```
51+
52+
getBalance 始终返回总金额,并且每次转账后总金额应相同
53+
54+
```
55+
private synchronized int getBalance() {
56+
int balance = 0;
57+
for (int account : accounts) {
58+
balance += account;
59+
}
60+
return balance;
61+
}
62+
}
63+
```
64+
65+
## 类图
66+
![alt text](./etc/monitor.urm.png "Monitor class diagram")
67+
68+
## 适用性
69+
70+
监视器设计模式应该用于具有需要由多个线程或进程同时访问和操作的共享资源的情况。 此模式在需要同步以防止竞争条件、数据损坏和不一致状态的情况下特别有用。 以下是您应该考虑使用监视器模式的一些情况:
71+
72+
1. **共享数据**:当您的应用程序涉及需要由多个线程访问和更新的共享数据结构、变量或资源时。 监视器确保一次只有一个线程可以访问共享资源,从而防止冲突并确保数据一致性。
73+
74+
2. **关键部分**:当您有代码的关键部分一次只需要由一个线程执行时。 关键部分是操作共享资源的代码部分,并发访问可能会导致问题。 监视器有助于确保在任何给定时间只有一个线程可以执行关键部分。
75+
76+
3. **线程安全**:当您需要确保线程安全而不是仅仅依赖锁和信号量等低级同步机制时。 监视器提供了封装同步和资源管理的更高级别的抽象。
77+
78+
4. **等待和发信号**:当您遇到线程需要等待满足某些条件才能继续操作时。 监视器通常包含线程等待特定条件以及其他线程在满足条件时通知它们的机制。
79+
80+
5. **死锁预防**:当您希望通过提供结构化方式来获取和释放共享资源上的锁来防止死锁时。 监视器通过确保资源访问得到良好管理来帮助避免常见的死锁情况。
81+
82+
6. **并发数据结构**:当您实现并发数据结构(例如队列、堆栈或哈希表)时,多个线程需要操作该结构,同时保持其完整性。
83+
84+
7. **资源共享**:当多个线程需要共享有限的资源时,例如连接数据库或访问网络套接字。 监视器可以帮助以受控方式管理这些资源的分配和释放。
85+
86+
8. **改进可维护性**:当您想要将同步逻辑和共享资源管理封装在单个对象中时,改进代码组织并使并发相关代码更容易推理。
87+
88+
但是,需要注意的是,监视器模式可能并不最适合所有并发场景。 在某些情况下,其他同步机制(例如锁、信号量或并发数据结构)可能更合适。 此外,现代编程语言和框架通常提供更高级别的并发结构,抽象出低级别同步的复杂性。
89+
90+
在应用监视器模式之前,建议彻底分析应用程序的并发需求,并选择最适合您需求的同步方法,同时考虑性能、复杂性和可用语言功能等因素。
91+
92+
## 相关模式
93+
94+
* Active object
95+
* Double-checked locking
10.2 KB
Loading

0 commit comments

Comments
 (0)