[TIL-3] SOLID Principles, Explanation and Example

What is SOLID Principles ?

S — Single Responsibility Principle (SRP)

a class should do one thing and therefore it should have only a single reason to change.

O — Open-Closed Principle (OCP)

classes should be open for extension and closed to modification

L — Liskov Substitution Principle (LSP)

A sub-class must be substitutable for its super-class

Let φ(x) be a property provable about objects x of type T. Then φ(y) should be true for objects y of type S where S is a subtype of T.

Functions that use pointers of references to base classes must be able to use objects of derived classes without knowing it.

Subclass/derived class should be substitutable for their base/parent class.

I — Interface Segregation Principle (ISP)

A Client should not be forced to implement an interface that it doesn’t use.

D — Dependency Inversion Principle (DIP)

High-level modules should not depend on low-level modules. Both should depend on abstractions

Abstractions should not depend on details. Details should depend on abstractions.

“If the OCP (Open-Closed Principle) states the goal of OO architecture, the DIP states the primary mechanism”.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store