OOD (Object-Oriented Design)

This Blog is basically an introduction to the principles of Object Oriented design and patterns as devised by Robert C. Martin (a.k.a Uncle Bob).

Whenever we start learning OOPs concept we go through basic concepts like polymorphism, data abstraction, inheritance, modularity etc, we get to know how to use them (the syntax) but they don’t teach us how to leverage them. Hence, in order to extract maximum advantage out of OOPs concept Uncle Bob devised S.O.L.I.D principles of object-oriented design.

In total, uncle Bob devised 11 OOD principles out of which first five are the principles for making a good class design.These five principles are the S.O.L.I.D principles and if followed by developers, helps them to create designs which are flexible to grow with time and easy to maintain with minimal changes.

The S.O.L.I.D principles are:

  1. S – The Single Responsibility Principle (SRP)
  2. O – Open Closed Principle (OCP)
  3. L – Liskov Substitution Principle (LSP)
  4. I – Interface Segregation Principle (ISP)
  5. D – Dependency Inversion Principle (DIP)

The Single Responsibility Principle (SRP)

According to this principle:

“A class should have one, and only one, reason to change or,
there should never be more than one reason for a class to change.”

This means that a class should only be created for handling a single responsibility i.e if there are more than one responsibility for a class then there will more than one reason to change it which will make it difficult to maintain the class .Using this principle we will be able to develop code which will be robust and easy to maintain and hence less possibility of Spaghetti Code.

Open Closed Principle (OCP)

“You should be able to extend a class behavior, without modifying it or, A class should be open for extension but closed to modification.”

This is divided into two statements-

A class should be open for extension – It basically means that a class should be ready to extend its functionalities whenever needed.

A class should be closed to modifications – It means that when extending the functionalities of a class, the already tested code should not be modified. This modification may lead to unexpected behavior of the clients of that class.

Liskov Substitution Principle (LSP)

Derived classes must be substitutable for their base classes.”

The idea behind this principle is that code should be written in such a way that whenever a derived class is created from the base class, there should not be any modification in the code to work with this derived class.

Interface Segregation Principle (ISP)

“Make fine grained interfaces that are client specific or, Clients
should not be forced to depend upon interfaces that they do not
use.”

This one is very least used principles among the five as it is based on the context in which it is being used. It basically means we should provide only that interface to client which contains only those methods which are to be used by the client i.e bulky interface (containing lot of methods) should be avoided.

Dependency Inversion Principle

“Depend on abstractions, not on concretions.”

It basically states that –

A. High level modules should not depend upon low level modules. Both should
depend upon abstractions.

B. Abstraction should not depend upon details. Details should depend upon abstractions.

There are many people who disagree with these principles but I would like to say that these are just the principles and one can use them to write good code and one should always be open to accept the changes required in his/her code.

Thanks Uncle Bob 🙂

Keep Coding!

Chao!

 

 

 

 

Leave a comment

Create a free website or blog at WordPress.com.

Up ↑