Skip to content

Commit eed25a4

Browse files
committed
Classes
1 parent 89e1d24 commit eed25a4

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

README.md

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,11 +1504,17 @@ class Square implements Shape {
15041504

15051505
## Classes
15061506

1507+
##
1508+
15071509
### Classes should be small
15081510

1511+
### 类应当小
1512+
15091513
The class' size is measured by it's responsibility. Following the *Single Responsibility principle* a class should be small.
15101514

1511-
**Bad:**
1515+
类的大小由它的职责衡量。 根据 *单一职责原则* 一个类应该小。
1516+
1517+
**不好的:**
15121518

15131519
```ts
15141520
class Dashboard {
@@ -1531,7 +1537,7 @@ class Dashboard {
15311537

15321538
```
15331539

1534-
**Good:**
1540+
**好的:**
15351541

15361542
```ts
15371543
class Dashboard {
@@ -1541,28 +1547,39 @@ class Dashboard {
15411547
}
15421548

15431549
// split the responsibilities by moving the remaining methods to other classes
1550+
// 将其它方法移到其它类以拆分职责
15441551
// ...
15451552
```
15461553

1547-
**[back to top](#table-of-contents)**
1554+
**[返回目录](#目录)**
15481555

15491556
### High cohesion and low coupling
15501557

1558+
### 高内聚和低耦合
1559+
15511560
Cohesion defines the degree to which class members are related to each other. Ideally, all fields within a class should be used by each method.
15521561
We then say that the class is *maximally cohesive*. In practice, this however is not always possible, nor even advisable. You should however prefer cohesion to be high.
15531562

15541563
Coupling refers to how related or dependent are two classes toward each other. Classes are said to be low coupled if changes in one of them doesn't affect the other one.
15551564

15561565
Good software design has **high cohesion** and **low coupling**.
15571566

1558-
**Bad:**
1567+
内聚定义了类成员彼此相关的程度。 理想情况下,每个方法都应使用类中的所有字段。
1568+
然后我们说这个类是*最高内聚*。 实际上, 这并非总是可行, 甚至不可取。 但是你应该更喜欢高内聚。
1569+
1570+
耦合指的是两个类相互依赖或相互依赖的程度。 如果其中一个类中的更改不影响其它的, 则称这些类是低耦合的。
1571+
1572+
好的软件设计拥有 **高内聚****低耦合**
1573+
1574+
**不好的:**
15591575

15601576
```ts
15611577
class UserManager {
15621578
// Bad: each private variable is used by one or another group of methods.
15631579
// It makes clear evidence that the class is holding more than a single responsibility.
15641580
// If I need only to create the service to get the transactions for a user,
15651581
// I'm still forced to pass and instance of `emailSender`.
1582+
// 不好的: 每一个私有变量仅仅被一组或另一组方法使用, 这很明显说明这个类在兼负多个职责。 当我仅仅需要一个服务来获取用户事务时, 还得传递另外一个 `emailSender` 的实例。
15661583
constructor(
15671584
private readonly db: Database,
15681585
private readonly emailSender: EmailSender) {
@@ -1590,7 +1607,7 @@ class UserManager {
15901607
}
15911608
```
15921609

1593-
**Good:**
1610+
**好的:**
15941611

15951612
```ts
15961613
class UserService {
@@ -1624,10 +1641,12 @@ class UserNotifier {
16241641
}
16251642
```
16261643

1627-
**[back to top](#table-of-contents)**
1644+
**[返回目录](#目录)**
16281645

16291646
### Prefer composition over inheritance
16301647

1648+
### 组合优先于继承
1649+
16311650
As stated famously in [Design Patterns](https://en.wikipedia.org/wiki/Design_Patterns) by the Gang of Four, you should *prefer composition over inheritance* where you can. There are lots of good reasons to use inheritance and lots of good reasons to use composition. The main point for this maxim is that if your mind instinctively goes for inheritance, try to think if composition could model your problem better. In some cases it can.
16321651

16331652
You might be wondering then, "when should I use inheritance?" It depends on your problem at hand, but this is a decent list of when inheritance makes more sense than composition:
@@ -1638,7 +1657,19 @@ You might be wondering then, "when should I use inheritance?" It depends on your
16381657

16391658
3. You want to make global changes to derived classes by changing a base class. (Change the caloric expenditure of all animals when they move).
16401659

1641-
**Bad:**
1660+
正如[*设计模式四人帮*](https://en.wikipedia.org/wiki/Design_Patterns)所述, 如果可能,
1661+
你应该优先使用组合而不是继承。 有许多好的理由去使用继承, 也有许多好的理由去使用组合。这个格言
1662+
的重点是, 如果你本能的观点是继承, 那么请想一下组合能否更好的为你的问题建模。 很多情况下它真的
1663+
可以。
1664+
1665+
那么你也许会这样想, “我什么时候改使用继承?” 这取决于你手上的问题, 不过这儿有一个像样的列表说
1666+
明什么时候继承比组合更好用:
1667+
1668+
1. 你的继承表示"是一个"的关系而不是"有一个"的关系(人类->动物 vs 用户->用户详情);
1669+
2. 你可以重用来自基类的代码(人可以像所有动物一样行动);
1670+
3. 你想通过基类对子类进行全局的修改(改变所有动物行动时的热量消耗);
1671+
1672+
**不好的:**
16421673

16431674
```ts
16441675
class Employee {
@@ -1651,6 +1682,7 @@ class Employee {
16511682
}
16521683

16531684
// Bad because Employees "have" tax data. EmployeeTaxData is not a type of Employee
1685+
// 不好是因为雇员“有”税率数据, EmployeeTaxData 不是一个 Employee 类型。
16541686
class EmployeeTaxData extends Employee {
16551687
constructor(
16561688
name: string,
@@ -1664,7 +1696,7 @@ class EmployeeTaxData extends Employee {
16641696
}
16651697
```
16661698

1667-
**Good:**
1699+
**不好的:**
16681700

16691701
```ts
16701702
class Employee {
@@ -1693,13 +1725,17 @@ class EmployeeTaxData {
16931725
}
16941726
```
16951727

1696-
**[back to top](#table-of-contents)**
1728+
**[返回目录](#目录)**
16971729

16981730
### Use method chaining
16991731

1732+
### 使用方法链
1733+
17001734
This pattern is very useful and commonly used in many libraries. It allows your code to be expressive, and less verbose. For that reason, use method chaining and take a look at how clean your code will be.
17011735

1702-
**Bad:**
1736+
这个模式在 JavaScript 中是非常有用的, 并且在许多类库使用。 它使你的代码变得富有表现力, 并减少啰嗦。 因为这个原因, 我说, 使用方法链然后再看看你的代码会变得多么简洁。
1737+
1738+
**不好的:**
17031739

17041740
```ts
17051741
class QueryBuilder {
@@ -1736,7 +1772,7 @@ queryBuilder.orderBy('firstName', 'lastName');
17361772
const query = queryBuilder.build();
17371773
```
17381774

1739-
**Good:**
1775+
**好的:**
17401776

17411777
```ts
17421778
class QueryBuilder {
@@ -1775,7 +1811,7 @@ const query = new QueryBuilder()
17751811
.build();
17761812
```
17771813

1778-
**[back to top](#table-of-contents)**
1814+
**[返回目录](#目录)**
17791815

17801816
## SOLID
17811817

0 commit comments

Comments
 (0)