六大设计原则

本文深入探讨了软件设计中的六大核心原则:单一职责原则、里氏替换原则、依赖倒置原则、接口隔离原则、迪米特法则及开闭原则,为开发者提供了提升代码质量和维护性的关键指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单一职责原则(Single Responsibility Principle)

There should never be more than one reason for a class to change.

总结,一个接口或者一个类,尽量做到,只做一件事情,清晰,明显。

里氏替换原则(Liskov Substitution Principle)

Software entities like classes,modules and functions should be open for extension but closed for modifications.

子类替换基类没有任何影响,类似继承的概念

依赖倒置原则(Dependence Inversion Principle,DIP)

High level modules should not depend upon low level modules.Both should depend upon abstractions.Abstractions should not depend upon details.Details should depend upon abstractions.

模块之间的依赖是通过抽象类或者接口,总结,就是面向接口编程

接口隔离原则

  1. Clients should not be forced to depend upon interfaces that they don’t use.
  2. The dependency of one class to another one should depend on the smallest possible interface.

通过不用业务模块,将接口分割出来,总结,接口尽量细化。

迪米特法则(Law of Demeter)

Only talk to your immediate friends

类之间解耦合,只要暴露和其他类习惯关联的方法,就是用public修饰,其他的用private,尽量高内聚。

开闭原则(Open Close Principle)

Software entities like classes,modules and functions should be open for extension but closed for modifications.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值