Sunday, June 13, 2021

SOLID-Design-Principles

SOLID-Design-Principles

The SOLID principles are fundamental to designing effective, maintainable, object-oriented systems. Whether you've only just begun writing software or have been doing so for years, these principles, when used appropriately, can improve the encapsulation and coupling of your application, making it more adaptable and testable in the face of changing requirements.

The purpose of SOLID design principles

  • To make the code more maintainable.
  • To make it easier to quickly extend the system with new functionality without breaking the existing ones.
  • To make the code easier to read and understand, thus spend less time figuring out what it does and more time actually developing the solution.

SOLID is one of the most popular sets of design principles in object-oriented software development. It’s a mnemonic acronym for the following five design principles:

  1. Single Responsibility Principle
  2. Open/Closed Principle
  3. Liskov Substitution Principle
  4. Interface Segregation Principle
  5. Dependency Inversion

All of them are broadly used and worth knowing.

1. Single Responsibility Principle

A class should have one, and only one, reason to change.

  • A class should only be responsible for one thing.
  • There's a place for everything and everything is in its place.
  • Find one reason to change and take everything else out of the class.
  • Very precise names for many small classes > generic names for large classes.

It works with all method functions and classes so it's all entities that we can imagine it's not only for classes. The main purpose is to have only one thing in there.

Goal: A clean and organized room.

2. Open/Closed Principle

A entity should be open for extension, but closed for modification.

  • An entity should be open for extension but closed for modification.
  • Extend functionality by adding new code instead of changing existing code.
  • Separate the behaviors, so the system can easily be extended, but never broken.

Goal: get to a point where you can never break the core of your system.

You can extend the functionality of system by adding new code instead of changing the existing one.

eg: open source library or package that you use

3. Liskov Substitution Principle

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.

We should able to change every concrete class instance in our code with anything that implements the same interface.

  • Any derived class should be able to substitute its parent class without the consumer knowing it.
  • Every class that implements an interface, must be able to substitute any reference throughout the code that implements that same interface.
  • Every part of the code should get the expected result no matter what instance of a class you send to it, given it implements the same interface.

eg: Changing database engine. File system to db, vice versa, etc.

4. Interface Segregation Principle

No client should be forced to depend on methods it does not use.

  • A client should never be forced to depend on methods it doesn't use.
  • Or, a client should never depend on anything more than the method it's calling.
  • Changing one method in a class shouldn't affect classes that don't depend on it.
  • Replace fat interfaces with many small, specific interfaces.

5. Dependency Inversion Principle

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

  • Never depend on anything concrete, only depend on abstractions.
  • High level modules should not depend on low level modules. They should depend on abstractions.
  • Able to change an implementation easily without altering the high level code.

    eg : changing database engine

! Don't get trapped by SOLID

• SOLID design principles are principles, not rules.

• Always use common sense when applying SOLID.

• Avoid over-fragmenting your code for the sake of SRP or SOLID.

• Don't try to achieve SOLID, use SOLID to achieve maintainability.

Final Thoughts

• The purpose of SOLID principles is to make your code more maintainable, easy to extend and reason about.

It requires spending more time writing code, so you can spend less reading it later.

• SOLID principles are principles, not rules.

• Always know your trade-offs and use common sense.

SOLID is your tool, not your goal.

2 comments:

  1. Thank you for this article because it’s really informative, I love reading your article and I hope that I will read some more about this stuff, it’s really informative and very entertaining. Thanks a lot and have a great day.

    Full Stack Java Developer - No coding experience required
    Authorized training center of Autodesk. It is the best Auto CAD training in Delhi NCR

    ReplyDelete
  2. Awesome Article Thanks for sharing your valuable information you can hire Fullstack developer from Technoduce in a flexible manner. Experienced Fullstack developer of the company to work at your or our place!

    ReplyDelete

SOLID-Design-Principles

SOLID-Design-Principles The SOLID principles are fundamental to designing effective, maintainable, object-oriented systems. Whether you...