Object Oriented Design Principles in Java

In this article, we will learn the Object-Oriented Design Principles/Guidelines which can be applied in our day to day project work. It's important to learn basics of Object-oriented programming like AbstractionEncapsulationPolymorphism, and Inheritance. But, at the same time, it's equally important to know object-oriented design principles, to create the clean and modular design. These guidelines helps design strong object-oriented software design.
Let's list important Object-Oriented Design Principles and we will discuss each guideline in detail.

Encapsulate What Varies

  • If you know your code is going to change in future then separate it out.
  • The benefit of this OOPS Design principle is that It's easy to test and maintain proper encapsulated code.
  • Only one thing is constant in software field and that is "Change", So encapsulate the code you expect or suspect to be changed in future.
  • If you are coding in Java then follow the principle of making variable and methods private by default and increasing access step by step e.g. from private to protected and not public. Several of the design patterns in Java uses Encapsulation, Factory design pattern is one example of Encapsulation which encapsulates object creation code and provides flexibility to introduce a new product later with no impact on existing code.

Code to an Interface Rather Than to an Implementation

  • Always program for an interface and not for implementation this will lead to flexible code which can work with any new implementation of an interface.
  • Use interface type on variables, return types of method or argument type of methods in Java. This has been advised by many Java programmer including in Effective Java and Head First design pattern book.

Delegation Principle

  • Don't do all stuff by yourself, delegate it to respective class. Classical example of delegation design principle is equals() and hashCode()method in Java.
  • In order to compare two objects for equality, we ask class itself to do comparison instead of Client class doing that check. The benefit of this design principle is no duplication of code and pretty easy to modify behaviour.
Read how delegation works in Java on Delegation in Java

The Open Closed Principle (OCP)

  • Classes should be open for extension but close for modification.
  • Classes, methods or functions should be Open for extension (new functionality) and Closed for modification.
  • The benefit of this Object oriented design principle is, which prevents someone from changing already tried and tested code.
Read more on Open Closed Principle

Dry- Don't Repeat Yourself

  • Avoid duplicate code by abstracting common things and place them in a single location.
  • Use an abstract class to abstract common things in one place. If you have a block of code in more than two places consider making it a separate method, or if you use a hard-coded value more than one time make them public static final constant.
  • The benefit of this Object oriented design principle is in maintenance.

Single Responsibility Principle (SRP)

  • Every object in our web application should have a single responsibility, and all object's services should be focused on carrying that single responsibility(SRP).
  • If you put more than one functionality in one Class in Java it introduces coupling between two functionality and even if you change one functionality there is a chance you broke coupled functionality, which requires another round of testing to avoid any surprise on the production environment.

Liskov's Substitution Principle (LSP)

  • A subclass should be suitable for their base classes.
  • According to the Liskov Substitution Principle, Subtypes must be substitutable for supertype i.e. methods or functions which uses superclass type must be able to work with the object of subclass without any issue".
  • LSP is closely related to Single responsibility principle and Interface Segregation Principle. If a class has more functionality than subclass might not support some of the functionality and does violate LSP.

Interface Segregation Principle (ISP)

  • Interface Segregation Principle stats 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 the client only needs one functionality and no other.Interface design is a tricky job because once you release your interface you can not change it without breaking all implementation.
  • Another benefit of this design principle in Java is, an interface has the disadvantage to implementing all method before any class can use it so having single functionality means less method to implement.

Dependency Injection or Inversion Principle

  • Don't ask for dependency it will be provided to you by a framework. This has been very well implemented in Spring framework, the beauty of this design principle is that any class which is injected by DI framework is easy to test with the mock object and easier to maintain because object creation code is centralized in the framework and client code is not littered with that.
  • There are multiple ways to implemented Dependency injection like using bytecode instrumentation which some AOP (Aspect Oriented programming) framework like AspectJ does or by using proxies just like used in Spring. See this example of IOC and DI design pattern to learn more about this SOLID design principle.

Related Posts