Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript 中的 DILOS 原则 #6

Open
gzqby opened this issue Nov 28, 2020 · 0 comments
Open

JavaScript 中的 DILOS 原则 #6

gzqby opened this issue Nov 28, 2020 · 0 comments

Comments

@gzqby
Copy link
Owner

gzqby commented Nov 28, 2020

#JavaScript 中的 DILOS 原则

DILOS 的含义:

  • D——依赖倒置反转原则
  • I——接口捆绑原则
  • L——里氏分离原则
  • O——开闭原则
  • S——多职责原则

依赖倒置反转原则

高级模块必须依赖低级模块。依赖实体而不是抽象。

function hipAPI(url, httpMethod) {
  ...
}
hipAPI('http://url.com', 'get');

function hipDifferentAPI(type, httpMethod) {
  if(type instanceof initialLoad) {

  }else if(type instanceof naBar) {

  }else {

  }
}

接口捆绑原则

强迫客户端依赖它们不用的 [代码]。
写的函数越少越好。把什么东西封装在一个放在其他地方的新函数里,并抽象化它的逻辑?可别这么干。怎么让人犯迷糊怎么来,需要代码的时候复制粘贴过来就行。

里氏分离原则

软件各部分的子级和父级不可以互换。
DRY。因为如果我们不遵循里氏分离原则,我们就会构建准确而健壮的继承链。将逻辑抽象为封装好的 base 原型 / 对象。我们还会对原型链中的不同方法按逻辑分组,它们的特定覆盖会让代码路径更加可预期和可发现。

开闭原则

对象应对修改开放,对扩展封闭。

function makeSound(animal) {
  if(animal === "dog"){
    ...
  }else if(animal === "cat") {
    ...
  }else if(animal === "crow") {
    ...
  }else if(animal === "sheep") {
    ...
  }else if(animal === "cow") {
    ...
  }else if(animal === "pig") {
    ...
  }else{
    ...
  }
}

多职责原则

确保你的函数 / 对象有多重职责。
优秀的编程人员常常会将他们的代码分成多个不同的对象或模块。但我可搞不清楚这种事情,我记不住它们都负责什么事情。
这种反模式有时被称为“瑞士军刀”,因为就算你要的只是一把剪子,但它也可以是指甲锉、锯子、镊子、开瓶器,也可以是软木钉。


FROM 极客时间 作者: Kealan Parr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant