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
Copy file name to clipboardExpand all lines: README.md
+48-12Lines changed: 48 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1504,11 +1504,17 @@ class Square implements Shape {
1504
1504
1505
1505
## Classes
1506
1506
1507
+
## 类
1508
+
1507
1509
### Classes should be small
1508
1510
1511
+
### 类应当小
1512
+
1509
1513
The class' size is measured by it's responsibility. Following the *Single Responsibility principle* a class should be small.
1510
1514
1511
-
**Bad:**
1515
+
类的大小由它的职责衡量。 根据 *单一职责原则* 一个类应该小。
1516
+
1517
+
**不好的:**
1512
1518
1513
1519
```ts
1514
1520
classDashboard {
@@ -1531,7 +1537,7 @@ class Dashboard {
1531
1537
1532
1538
```
1533
1539
1534
-
**Good:**
1540
+
**好的:**
1535
1541
1536
1542
```ts
1537
1543
classDashboard {
@@ -1541,28 +1547,39 @@ class Dashboard {
1541
1547
}
1542
1548
1543
1549
// split the responsibilities by moving the remaining methods to other classes
1550
+
// 将其它方法移到其它类以拆分职责
1544
1551
// ...
1545
1552
```
1546
1553
1547
-
**[⬆ back to top](#table-of-contents)**
1554
+
**[⬆ 返回目录](#目录)**
1548
1555
1549
1556
### High cohesion and low coupling
1550
1557
1558
+
### 高内聚和低耦合
1559
+
1551
1560
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.
1552
1561
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.
1553
1562
1554
1563
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.
1555
1564
1556
1565
Good software design has **high cohesion** and **low coupling**.
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.
1632
1651
1633
1652
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
1638
1657
1639
1658
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).
@@ -1664,7 +1696,7 @@ class EmployeeTaxData extends Employee {
1664
1696
}
1665
1697
```
1666
1698
1667
-
**Good:**
1699
+
**不好的:**
1668
1700
1669
1701
```ts
1670
1702
classEmployee {
@@ -1693,13 +1725,17 @@ class EmployeeTaxData {
1693
1725
}
1694
1726
```
1695
1727
1696
-
**[⬆ back to top](#table-of-contents)**
1728
+
**[⬆ 返回目录](#目录)**
1697
1729
1698
1730
### Use method chaining
1699
1731
1732
+
### 使用方法链
1733
+
1700
1734
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.
0 commit comments