• 单一职责原则 (SRP)
    • 开/闭原则 (OCP)
    • 利斯科夫替代原则 (LSP)
    • 接口隔离原则 (ISP)
    • 依赖反转原则 (DIP)
    • 使用 ES6 的 classes 而不是 ES5 的 Function
    • 使用方法链
    • 优先使用组合模式而非继承

    单一职责原则 (SRP)

    如《代码整洁之道》一书中所述,“修改一个类的理由不应该超过一个”。

    将多个功能塞进一个类的想法很诱人,但这将导致你的类无法达到概念上的内聚,并经常不得不进行修改。

    最小化对一个类需要修改的次数是非常有必要的。如果一个类具有太多太杂的功能,当你对其中一小部分进行修改时,将很难想象到这一修够对代码库中依赖该类的其他模块会带来什么样的影响。

    反例:

    1. class UserSettings {
    2. constructor(user) {
    3. this.user = user;
    4. }
    5. changeSettings(settings) {
    6. if (this.verifyCredentials(user)) {
    7. // ...
    8. }
    9. }
    10. verifyCredentials(user) {
    11. // ...
    12. }
    13. }

    正例:

    1. class UserAuth {
    2. constructor(user) {
    3. this.user = user;
    4. }
    5. verifyCredentials() {
    6. // ...
    7. }
    8. }
    9. class UserSettings {
    10. constructor(user) {
    11. this.user = user;
    12. this.auth = new UserAuth(user)
    13. }
    14. changeSettings(settings) {
    15. if (this.auth.verifyCredentials()) {
    16. // ...
    17. }
    18. }
    19. }

    开/闭原则 (OCP)

    “代码实体(类,模块,函数等)应该易于扩展,难于修改。”

    这一原则指的是我们应允许用户方便的扩展我们代码模块的功能,而不需要打开 js 文件源码手动对其进行修改。

    反例:

    1. class AjaxRequester {
    2. constructor() {
    3. // What if we wanted another HTTP Method, like DELETE? We would have to
    4. // open this file up and modify this and put it in manually.
    5. this.HTTP_METHODS = ['POST', 'PUT', 'GET'];
    6. }
    7. get(url) {
    8. // ...
    9. }
    10. }

    正例:

    1. class AjaxRequester {
    2. constructor() {
    3. this.HTTP_METHODS = ['POST', 'PUT', 'GET'];
    4. }
    5. get(url) {
    6. // ...
    7. }
    8. addHTTPMethod(method) {
    9. this.HTTP_METHODS.push(method);
    10. }
    11. }

    利斯科夫替代原则 (LSP)

    “子类对象应该能够替换其超类对象被使用”。

    也就是说,如果有一个父类和一个子类,当采用子类替换父类时不应该产生错误的结果。

    反例:

    1. class Rectangle {
    2. constructor() {
    3. this.width = 0;
    4. this.height = 0;
    5. }
    6. setColor(color) {
    7. // ...
    8. }
    9. render(area) {
    10. // ...
    11. }
    12. setWidth(width) {
    13. this.width = width;
    14. }
    15. setHeight(height) {
    16. this.height = height;
    17. }
    18. getArea() {
    19. return this.width * this.height;
    20. }
    21. }
    22. class Square extends Rectangle {
    23. constructor() {
    24. super();
    25. }
    26. setWidth(width) {
    27. this.width = width;
    28. this.height = width;
    29. }
    30. setHeight(height) {
    31. this.width = height;
    32. this.height = height;
    33. }
    34. }
    35. function renderLargeRectangles(rectangles) {
    36. rectangles.forEach((rectangle) => {
    37. rectangle.setWidth(4);
    38. rectangle.setHeight(5);
    39. let area = rectangle.getArea(); // BAD: Will return 25 for Square. Should be 20.
    40. rectangle.render(area);
    41. })
    42. }
    43. let rectangles = [new Rectangle(), new Rectangle(), new Square()];
    44. renderLargeRectangles(rectangles);

    正例:

    1. class Shape {
    2. constructor() {}
    3. setColor(color) {
    4. // ...
    5. }
    6. render(area) {
    7. // ...
    8. }
    9. }
    10. class Rectangle extends Shape {
    11. constructor() {
    12. super();
    13. this.width = 0;
    14. this.height = 0;
    15. }
    16. setWidth(width) {
    17. this.width = width;
    18. }
    19. setHeight(height) {
    20. this.height = height;
    21. }
    22. getArea() {
    23. return this.width * this.height;
    24. }
    25. }
    26. class Square extends Shape {
    27. constructor() {
    28. super();
    29. this.length = 0;
    30. }
    31. setLength(length) {
    32. this.length = length;
    33. }
    34. getArea() {
    35. return this.length * this.length;
    36. }
    37. }
    38. function renderLargeShapes(shapes) {
    39. shapes.forEach((shape) => {
    40. switch (shape.constructor.name) {
    41. case 'Square':
    42. shape.setLength(5);
    43. case 'Rectangle':
    44. shape.setWidth(4);
    45. shape.setHeight(5);
    46. }
    47. let area = shape.getArea();
    48. shape.render(area);
    49. })
    50. }
    51. let shapes = [new Rectangle(), new Rectangle(), new Square()];
    52. renderLargeShapes(shapes);

    接口隔离原则 (ISP)

    “客户端不应该依赖它不需要的接口;一个类对另一个类的依赖应该建立在最小的接口上。”

    在 JS 中,当一个类需要许多参数设置才能生成一个对象时,或许大多时候不需要设置这么多的参数。此时减少对配置参数数量的需求是有益的。

    反例:

    1. class DOMTraverser {
    2. constructor(settings) {
    3. this.settings = settings;
    4. this.setup();
    5. }
    6. setup() {
    7. this.rootNode = this.settings.rootNode;
    8. this.animationModule.setup();
    9. }
    10. traverse() {
    11. // ...
    12. }
    13. }
    14. let $ = new DOMTraverser({
    15. rootNode: document.getElementsByTagName('body'),
    16. animationModule: function() {} // Most of the time, we won't need to animate when traversing.
    17. // ...
    18. });

    正例:

    1. class DOMTraverser {
    2. constructor(settings) {
    3. this.settings = settings;
    4. this.options = settings.options;
    5. this.setup();
    6. }
    7. setup() {
    8. this.rootNode = this.settings.rootNode;
    9. this.setupOptions();
    10. }
    11. setupOptions() {
    12. if (this.options.animationModule) {
    13. // ...
    14. }
    15. }
    16. traverse() {
    17. // ...
    18. }
    19. }
    20. let $ = new DOMTraverser({
    21. rootNode: document.getElementsByTagName('body'),
    22. options: {
    23. animationModule: function() {}
    24. }
    25. });

    依赖反转原则 (DIP)

    该原则有两个核心点:

    1. 高层模块不应该依赖于低层模块。他们都应该依赖于抽象接口。
    2. 抽象接口应该脱离具体实现,具体实现应该依赖于抽象接口。

    反例:

    1. class InventoryTracker {
    2. constructor(items) {
    3. this.items = items;
    4. // BAD: We have created a dependency on a specific request implementation.
    5. // We should just have requestItems depend on a request method: `request`
    6. this.requester = new InventoryRequester();
    7. }
    8. requestItems() {
    9. this.items.forEach((item) => {
    10. this.requester.requestItem(item);
    11. });
    12. }
    13. }
    14. class InventoryRequester {
    15. constructor() {
    16. this.REQ_METHODS = ['HTTP'];
    17. }
    18. requestItem(item) {
    19. // ...
    20. }
    21. }
    22. let inventoryTracker = new InventoryTracker(['apples', 'bananas']);
    23. inventoryTracker.requestItems();

    正例:

    1. class InventoryTracker {
    2. constructor(items, requester) {
    3. this.items = items;
    4. this.requester = requester;
    5. }
    6. requestItems() {
    7. this.items.forEach((item) => {
    8. this.requester.requestItem(item);
    9. });
    10. }
    11. }
    12. class InventoryRequesterV1 {
    13. constructor() {
    14. this.REQ_METHODS = ['HTTP'];
    15. }
    16. requestItem(item) {
    17. // ...
    18. }
    19. }
    20. class InventoryRequesterV2 {
    21. constructor() {
    22. this.REQ_METHODS = ['WS'];
    23. }
    24. requestItem(item) {
    25. // ...
    26. }
    27. }
    28. // By constructing our dependencies externally and injecting them, we can easily
    29. // substitute our request module for a fancy new one that uses WebSockets.
    30. let inventoryTracker = new InventoryTracker(['apples', 'bananas'], new InventoryRequesterV2());
    31. inventoryTracker.requestItems();

    使用 ES6 的 classes 而不是 ES5 的 Function

    典型的 ES5 的类(function)在继承、构造和方法定义方面可读性较差。

    当需要继承时,优先选用 classes。

    但是,当在需要更大更复杂的对象时,最好优先选择更小的 function 而非 classes。

    反例:

    1. var Animal = function(age) {
    2. if (!(this instanceof Animal)) {
    3. throw new Error("Instantiate Animal with `new`");
    4. }
    5. this.age = age;
    6. };
    7. Animal.prototype.move = function() {};
    8. var Mammal = function(age, furColor) {
    9. if (!(this instanceof Mammal)) {
    10. throw new Error("Instantiate Mammal with `new`");
    11. }
    12. Animal.call(this, age);
    13. this.furColor = furColor;
    14. };
    15. Mammal.prototype = Object.create(Animal.prototype);
    16. Mammal.prototype.constructor = Mammal;
    17. Mammal.prototype.liveBirth = function() {};
    18. var Human = function(age, furColor, languageSpoken) {
    19. if (!(this instanceof Human)) {
    20. throw new Error("Instantiate Human with `new`");
    21. }
    22. Mammal.call(this, age, furColor);
    23. this.languageSpoken = languageSpoken;
    24. };
    25. Human.prototype = Object.create(Mammal.prototype);
    26. Human.prototype.constructor = Human;
    27. Human.prototype.speak = function() {};

    正例:

    1. class Animal {
    2. constructor(age) {
    3. this.age = age;
    4. }
    5. move() {}
    6. }
    7. class Mammal extends Animal {
    8. constructor(age, furColor) {
    9. super(age);
    10. this.furColor = furColor;
    11. }
    12. liveBirth() {}
    13. }
    14. class Human extends Mammal {
    15. constructor(age, furColor, languageSpoken) {
    16. super(age, furColor);
    17. this.languageSpoken = languageSpoken;
    18. }
    19. speak() {}
    20. }

    使用方法链

    这里我们的理解与《代码整洁之道》的建议有些不同。

    有争论说方法链不够干净且违反了德米特法则,也许这是对的,但这种方法在 JS 及许多库(如 JQuery)中显得非常实用。

    因此,我认为在 JS 中使用方法链是非常合适的。在 class 的函数中返回 this,能够方便的将类需要执行的多个方法链接起来。

    反例:

    1. class Car {
    2. constructor() {
    3. this.make = 'Honda';
    4. this.model = 'Accord';
    5. this.color = 'white';
    6. }
    7. setMake(make) {
    8. this.name = name;
    9. }
    10. setModel(model) {
    11. this.model = model;
    12. }
    13. setColor(color) {
    14. this.color = color;
    15. }
    16. save() {
    17. console.log(this.make, this.model, this.color);
    18. }
    19. }
    20. let car = new Car();
    21. car.setColor('pink');
    22. car.setMake('Ford');
    23. car.setModel('F-150')
    24. car.save();

    正例:

    1. class Car {
    2. constructor() {
    3. this.make = 'Honda';
    4. this.model = 'Accord';
    5. this.color = 'white';
    6. }
    7. setMake(make) {
    8. this.name = name;
    9. // NOTE: Returning this for chaining
    10. return this;
    11. }
    12. setModel(model) {
    13. this.model = model;
    14. // NOTE: Returning this for chaining
    15. return this;
    16. }
    17. setColor(color) {
    18. this.color = color;
    19. // NOTE: Returning this for chaining
    20. return this;
    21. }
    22. save() {
    23. console.log(this.make, this.model, this.color);
    24. }
    25. }
    26. let car = new Car()
    27. .setColor('pink')
    28. .setMake('Ford')
    29. .setModel('F-150')
    30. .save();

    优先使用组合模式而非继承

    在著名的设计模式一书中提到,应多使用组合模式而非继承。

    这么做有许多优点,在想要使用继承前,多想想能否通过组合模式满足需求吧。

    那么,在什么时候继承具有更大的优势呢?这取决于你的具体需求,但大多情况下,可以遵守以下三点:

    1. 继承关系表现为”是一个”而非”有一个”(如动物->人 和 用户->用户细节)
    2. 可以复用基类的代码(“Human”可以看成是”All animal”的一种)
    3. 希望当基类改变时所有派生类都受到影响(如修改”all animals”移动时的卡路里消耗量)

    反例:

    1. class Employee {
    2. constructor(name, email) {
    3. this.name = name;
    4. this.email = email;
    5. }
    6. // ...
    7. }
    8. // Bad because Employees "have" tax data. EmployeeTaxData is not a type of Employee
    9. class EmployeeTaxData extends Employee {
    10. constructor(ssn, salary) {
    11. super();
    12. this.ssn = ssn;
    13. this.salary = salary;
    14. }
    15. // ...
    16. }

    正例:

    1. class Employee {
    2. constructor(name, email) {
    3. this.name = name;
    4. this.email = email;
    5. }
    6. setTaxData(ssn, salary) {
    7. this.taxData = new EmployeeTaxData(ssn, salary);
    8. }
    9. // ...
    10. }
    11. class EmployeeTaxData {
    12. constructor(ssn, salary) {
    13. this.ssn = ssn;
    14. this.salary = salary;
    15. }
    16. // ...
    17. }