SOLID Principles of Object-Oriented Programming

Introduction

In object-oriented computer programming, SOLID is a mnemonic acronym for five design principles intended to make software designs more understandable, flexible and maintainable. It is not related to the GRASP software design principles.

Quoting from SOLID – wiki

S – O – L – I – D

S – Single Responsibility Principle

A class should only have a single responsibility, that is, only changes to one part of the software’s specification should be able to affect the specification of the class.

O – Open–Closed Principle

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.

在面向对象编程中, 开放封闭原则的核心思想是”软件实体应该是可扩展, 而不可修改. 也就是说, 对扩展是开放的,而对修改是封闭的.”

Quoting from Open-Closed Principle – wikipedia

开放/封闭原则(Open-Closed principle)是面向对象设计的基本原则之一(SOLID中的O) ,声明一个软件实体应该对扩展是开放的, 对修改则是封闭的。

关于开放封闭原则,其核心的思想是:
软件实体应该是可扩展,而不可修改的。也就是说,对扩展是开放的,而对修改是封闭的。
因此,开放封闭原则主要体现在两个方面:
对扩展开放,意味着有新的需求或变化时,可以对现有代码进行扩展,以适应新的情况。
对修改封闭,意味着类一旦设计完成,就可以独立完成其工作,而不要对类进行任何修改。

L – Liskov Substitution Principle

“Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.” See also design by contract.

I – Interface Segregation Principle

“Many client-specific interfaces are better than one general-purpose interface.”

D – Dependency inversion principle

References

SOLID – wikipedia
Single Responsibility Principle – wikipedia
Open-Closed Principle – wikipedia
Liskov Substitution Principle – wikipedia
Interface Segregation Principle – wikipedia
Dependency inversion principle – wikipedia

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据