美文网首页
代码设计原则笔记

代码设计原则笔记

作者: 天之見證 | 来源:发表于2020-07-02 22:06 被阅读0次
简写 原则 中文 备注
SRP The Single Responsibility Principle 单一职责原则 一个类,只干自己件事
OCP The Open-Closed Principle 开闭原则 不会对现有的东西造成影响, 使用接口
LSP The Liskov Substitution Principle 里氏替换原则 子替父也是可行的
DIP The Dependency Inversion Principle 依赖反转原则 依赖接口,服务上层
ISP The Interface Segregation Principle 接口隔离原则 为了避免过度使用接口,对接口瘦身

0. 总结

以上5个原则里面,除了LSP这个,其他几个其实都是相互关联的, 例如,你如果不SRP的话, ISP就不好搞

  1. 不干太多的事 -> SRP
  2. 不打扰前任 -> OCP
  3. 我儿子来也行 -> LSP
  4. 我要的,你提供 -> DIP
  5. 接口虽好,不要贪杯 -> ISP

1. SRP

A class should have only one reason to change

Responsibility: a reason to change

modem_java.PNG
sep_modem.PNG

2. OCP

Software entities (classes, modules, functions, etc) should be open for extension, but closed for modification

not_oc.PNG
oc.PNG

3. LSP

Subtypes must be substitutable for their base types

rectangle_square_lsp.PNG

IS a is about Behavior

Design by Contract

Specifying Contract in UnitTest

4. DIP

  1. High-level modules should not depend on low-level modules. Both should depend on abstractions
  2. Abstractions should not depend on details. Details should depend on abstractions
no_dip.PNG
dip.PNG

Depend on Abstractions

5. ISP

There are objects that require noncohesive interfaces; however, it suggests that client should not know about them as a single class. Instead

clients should know about abstract base classes that have cohesive interfaces

no_isp.PNG
isp.PNG

ref: Agile Software Development Principles Patterns And Practices: section 2

相关文章

  • Template Method 模板方法模式

    设计原则学习笔记 设计模式学习笔记 作用 将可变的和不可变的代码分离,可变的代码由子类实现,不可变代码由父类实现...

  • 代码设计原则笔记

    简写原则中文备注SRPThe Single Responsibility Principle单一职责原则一个类,只...

  • Bridge 桥接模式

    设计原则学习笔记 设计模式学习笔记 作用 以持有的方式实现接口,避免继承太多导致扩展性变差,同时实现代码的复用。...

  • 设计原则

    1.什么是设计原则。    设计原则是我们写出优质代码的指导思想。写代码容易,写好代码难。遵循设计原则,会使我们的...

  • 代码设计原则

    1、DRY原则DRY是 Don't repeat yourself 的缩写,意思是"不要重复自己"。它的涵义是,系...

  • 浅谈C++ 物理设计

    设计原则代码规范最佳实践附录: 实用宏 设计原则 自满足原则 所有头文件都应该自满足的。看一个具体的示例代码,这里...

  • 设计模式开篇

    设计模式与原则 设计模式原则是为了提高代码的可维护性,可复用性和可扩展性,设计模式是设计模式原则的具体体现。 设计...

  • 2021-09-12 web设计模式解析与实战(一)

    设计模式 有利于 组织模块 设计沟通 代码质量 五大设计原则: 开闭原则(OCP:open close princ...

  • 设计模式——“依赖倒置”原则

    目录 查看其它设计模式笔记,点这里→设计模式笔记汇总 依赖倒置原则(Dependence Inversion Pr...

  • 设计模式(-)

    设计模式的7大基本原则: 设计模式的原则:构建可以服用,可以维护代码的经验法则 原则(一)接口隔离原则: 接...

网友评论

      本文标题:代码设计原则笔记

      本文链接:https://www.haomeiwen.com/subject/rspdqktx.html