Object Oriented Design Principles

In an Object Oriented System, poorly designed classes spell catastrophe. Robert Martin listed some of the basic design principles in early 2000’s. these principles were to act as guidelines for developers in order to avoid bad design. The first 5 of these were very basic and they are now generally accepted as  Object Oriented Design Principles and popularly known as S.O.L.I.D design principles.

Before delving into SOLID design principles let us list out characteristics of bad design

  1. Rigidity  -Making changes are difficult as to change one class requires us to modify lots of other classes.
  2. Fragility – One change breaks many parts of system.
  3. Immobility – It is hard to reuse classes as it is highly tangled with other part of system.

S.O.L.I.D Design Principle

SOLID stands for SRP, OCP, LSP, ISP and DIP

  1. Single Responsibility Principle (SRP) –  A class should have only one reason to change. Each class should have only one responsibility and hence only one reason to change. If it has more than one reason to change then it should be what happens if a woman takes viagra split into two classes. The reason behind this is that if class with more than one responsibility needs to change then it will affect a larger part of system.
  2. Open Close Principle (OCP) – A class should be open to extension but close for modification. When ever you want to change behavior of your classes, library or packages etc then instead of modifying them just extend them. This ensures backward compatibility.
  3. Liskov Substitution Principle (LSP) – Substitutibility is a principle of Object Oriented design. This means that is S is a subtype of T then any object of type T can be replaced by objects of type S without any alteration of code. This is basis of Liksov Substitution Principle. In other words a Derived class should be written in such a way that it should be able to be used instead of Base class without changing the code.
  4. Interface segregation Principle (ISP) – ISP states that no client should be forced to use an interface that is has no use of.  This principle helps us in designing our interfaces. So we should add only those methods in an interface which has to be used by client. ISP tells us to break a large interface into smaller one so that client has to worry about only those methods which it will use. This keeps the code clean, decoupled and easily maintainable.
  5. Dependency Inversion Principle (DIP) – A high level module should not depend upon low level modules. Both should depend upon abstractions and abstraction should not depend on details, details should depend on abstraction. DIP states that high level module should be decoupled from low level modules by creating a layer of abstraction. Layer of abstraction is a wrapper on low level modules. This layer is created by taking into account the needs of high level modules. Thus abstraction(idea) drives the details and not vice-versa.

These are the famous SOLID design principles. Apart from SOLID there are few other notable designs which are mentions now and then.

YAGNI (You aren’t gonna need it) – This is not a object oriented design per se. This is rather a agile principle for software implementation. You should write only that amount of code what you need. If you write code thinking about your future need then you will end up with bloated, unusable code. And who knows how the design will change in future.

DRY (Don’t repeat yourself) – This is another neat design principle which says that do not write duplicate code. So if you see yourself writing duplicate code then wrap that code in a class (or a method) and start reusing it.

KISS(Keep it simple,stupid) – This is more generic and not a OOP design. This to me is a simple way of rephrasing SRP principle. Your classes should be simple and should do only what it is suppose to do. If it is complicated then break it into multiple classes unless you achieve simplicity.

That is all folks. Do remember to follow these guidelines while designing your classes. This will often make you a coder.  As it is understood, all the comments, negative or positive are most welcome.

And all the spammers STFU.

Warm Regards


Print Friendly, PDF & Email

About Niraj Singh

I am CEO and CoFounder of a startup "Aranin Software Private Limited, Bangalore. I completed my graduation in 2002 as an Aerospace Engineer from IIT Kharagpur. I love working on new ideas and projects and recently released my first open source project JaiomServer "http://jaiomserver.org". I have 9 years of experience in IT industries most of which I have spent in developing community applications for various clients using java. Some of the sites in which I have actively involved with are hgtv.com, food.com, foodnetwork.com, pickle.com, diynetwork.com etc.
This entry was posted in Design and tagged , , . Bookmark the permalink.