Day 04: 进入主题前的补充:SOLID

为什麽要补充?

当决定铁人赛的题目是 Design Patterns 时,除了先 Google 看看网路的文章、心得之外,我会去看天珑书店的网站,寻找该领域的专业书籍,於是乎,我先找寻「Design Patterns: Elements of Reusable Object-Oriented Software」,当然,时间有限的情况下,优先选择中文版本,在网页上除了「物件导向设计模式」之外,还另外推荐两本书,分别是「深入浅出设计模式」与「大话设计模式」。购入这三本书之後,「物件导向设计模式」翻没几页就先暂时搁下,没有背景知识下阅读起来不轻松。反倒是「深入浅出设计模式」与「大话设计模式」则完全不同,在浅显易懂的文字带领下,慢慢领略、欣赏设计模式的美好。

其中,有五个原则这两本书中不断被提及,搭配 Google 後才了解到,SOLID,就是这五个原则取第一个字母组合而成,实际阅读、理解後,觉得这些原则可说是设计模式之所以这样实作的前提,可以这麽说,遵守 SOLID 原则的不一定有某个模式,而实作某个模式的,则可以看到 SOLID 的身影。

看来得好好了解五个原则。

出处

参考中文 Wiki 资料(连结),提倡者是 Clean Code 的作者 Robert C. Martin。

S, Single Responsibility Principle, SRP,单一职责原则

英文 Wiki 定义(连结):

The single-responsibility principle (SRP) is a computer-programming principle that states that every module, class or function in a computer program should have responsibility over a single part of that program's functionality, and it should encapsulate that part. All of that module, class or function's services should be narrowly aligned with that responsibility.

简单来说,让模组、类别或是函式的功能单一化,不要有包山包海的功能。用商学院的概念,可以联想到「Jobs To Be Done」,人们需要这个产品是因为要完成某件事。同理,当使用模组、类别或是函式时,是因为要完成某个需求才呼叫它们。

O, The Open/Closed Principle, OCP,开放-封闭原则

英文 Wiki 定义(连结):

In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"; that is, such an entity can allow its behaviour to be extended without modifying its source code.

简单来说,模组、类别或是函式对於扩充是容易可行的,同时,扩充的时候不会,也不需要更动既有的程序码。能够达成此原则的做法是维持低耦合,避免开发新功能时得一并更新旧的程序码。

L, Liskov Substitution Principle, LSP,里氏替换原则

英文 Wiki 定义(连结):

Liskov substitution principle (LSP), or Substitutability is a principle in object-oriented programming stating that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., an object of type T may be substituted with any object of a subtype S) without altering any of the desirable properties of the program (correctness, task performed, etc.).

简单来说,关於物件的继承部分。如果程序的运作一切正常,那具有子代的亲代物件可以被取代,程序的运作不会受到影响。用现实物品来讲,三菱的转转笔,不论使用第一代还是第六代,都可以达到书写的目的。

I, Interface Segregation Principle, ISP,介面隔离原则

英文 Wiki 定义(连结):

In the field of software engineering, the interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use. ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them.

简单来说,避免制造出通用的 Interface,反而是尽可能制造小的 Interface 好满足特定需求。用现实来说,动物的跑步,不该用一个包含全部的 Interface,反而要设计两条腿跑步的 Interface、四条腿的、昆虫的等等。
这段 JavaScript 开发者来说有点陌生,在物件导向语言中,因为大部分只允许单一物件继承,但偏偏有些方法来自不同物件时,可以使用 Interface 存放将不同类别的共通拥有的方法,且 Interface 允许多重实作,这带来的好处是提供开发者开发上的弹性。

D, Dependency Inversion Principle, DIP,依赖反转原则

英文 Wiki 定义(连结):

In object-oriented design, the dependency inversion principle is a specific form of loosely coupling software modules. When following this principle, the conventional dependency relationships established from high-level, policy-setting modules to low-level, dependency modules are reversed, thus rendering high-level modules independent of the low-level module implementation details.

简单来说,在设计物件的继承上,尽可能将概念性的内容取出成抽象性的东西(像是 Abstract Class 或 Interface)。在开发上因为将精华的部分萃取出来,後续在实作上、继承上能尽可能避免高耦合的情况。
这段 JavaScript 开发者来说有点陌生、不好理解,因为 JavaScript 没有 Abstract Class、Interface,导致後续实作 Patterns 时,JavaScript 将省略这一块。

总结

对 JavaScript 开发者来说,这五个原则中,SRP 和 OCP 是相对容易遵守的,另外三个则因为语言特色(缺乏虚拟层)导致不太容易遵守。但这没影响,原则的功用是了解概念後学习如何运用,不需要墨守成规。


<<:  Day5:如何使用Parrot Security的DNSRecon工具查询反向DNS资讯

>>:  [Day 03] if条件、缩排规则、函式写法,以及一些字串技巧

# Day#18 设定、上传照片与default值

前言 昨天只写了注册与登入的功能,好像完全没有提到这到底是个什麽样的app... 那天,没有朋友的我...

Day2 Android - 系统预设程序码

来记录一下自己学习的历程及遇到的问题,应该有人会觉得是废话啦~,那麽就开始今天的主题,在一开始建完p...

来举例一下 Neo4j 的实务应用

前情提要 最後一篇正篇,稍稍回顾了一下之前的每一篇 发现对於现实上的使用案例,好像没有太多的描述 所...

[Day05] - 新拟物风按钮(三) - 参数设定

昨天我们利用 shadow-dom 将元件内外的样式区隔开来 不过 , 目前的 neuomorphi...

[Day13] 团队系统设计-估点技巧

前面的文章讨论过估点与专案管理、风险控管的高度相关性。对於较年轻的工程师来说,估点是一个容易产生挫折...