Skip navigation

Category Archives: OOD

First OOD principle is “DRY”, as name suggest don’t repeat yourself -> DON’T write duplicate code
+easy to maintain
+less to test

Encapsulate What Changes (or will change)
A universal constant of software is change or Evolution (up to you).
So… encapsulate the code you expect to be changed in future… (tomorrow)

Open-Closed Design Principle
Classes, methods or functions should be OPEN for extension (new functionality) and CLOSED for modification.
This is a SOLID design principle, which prevents someone (your Cat for instance) from changing already tested code.

Single Responsibility Principle (SRP)
As per SRP, there should not be more than one reason for a class to change, or a class should always handle single functionality.
If you put more than one functionality in one Class in Java  it introduce coupling between two functionality and even if you change one functionality there is chance you broke coupled functionality,  which require another round of testing to avoid any surprise on production environment.
It represents “S” on SOLID acronym.

Dependency Injection or Inversion principle (Spring Love – IOC)
Don’t ask for dependency it will be provided to you by framework.
It represent “D” on SOLID acronym.

Favor Composition over Inheritance
Always favor composition over inheritance.

Liskov Substitution Principle (LSP)
Subtypes must be substitutable for super type.
Derived class or sub class MUST enhance functionality NOT reduce them.
LSP represent  “L” on SOLID acronym.

Interface Segregation principle (ISP)
ISP sostains that, a client should not implement an interface, if it doesn’t use that.
This happens mostly when one interface contains more than one functionality, and client only need one functionality and not other.
P.D: Interface design is tricky job because once you release your interface you can not change it without breaking all implementation.

Programming for Interface not implementation
Try to program for interface over implementation. This will lead to flexible code which can work with any new implementation of interface.
So use interface type on variables, return types of method or argument type of methods in Java.

Delegation principle
Don’t do all yourself, delegate it to respective class
(Example:like equals() and hashCode() Java things.In order to compare two object for equality we ask class itself to do comparison instead of Client class doing that check).


So as final note.:Try to keep all the principles in mind and use the common sense as a final OODP

modified from: