stock market

SOLID Design Principles in Java – A Complete Guide with Examples

Spread the love

SOLID is a set of five object-oriented design principles that help developers design clean, maintainable, scalable, and flexible software systems. These principles reduce tight coupling between classes, improve code readability, and make applications easier to extend and test.

  • S :Single Responsibility Principle (SRP)
  • O:Open/Closed Principle (OCP)
  • L:Liskov Substitution Principle (LSP)
  • I : Interface Segregation Principle (ISP)
  • D:Dependency Inversion Principle (DIP)

1.Single Responsibility Principle (SRP)

there should never be more than one functionality for a class to change , single responsibility focused , single logic/functionality addresses a specific concern .

Example:

We have written a code in a single class that code is actually creating and sending a message to remote server . that listing on some port .

What are the possible reason to change the class .

  • if communication protocol change then we have to change the class .
  • message format is change then we have to change the format . (JSON == > XML)
  • if Authentication mechanism change then we have to changed the class.
  • conclusion is that Single responsibility principle telling you have multiple responsibility then you have to create multiple classes .

1.UserController.java

  1. User.java

2.UserpersistenceService.java

4.Store.java

5.UserValidaor.java

2. Open Closed Principle

Open-closed principle should be open for extension but closed for modification . open for extension means extended existing behavior. closed for modification means existing code remain un change.

This means:

  • You should be able to add new behavior
  • Without changing existing, tested code

Suppose you have written one method in base class , you want to modify that logic we should override in child class and change the logic .

For example, consider a use case where a discount module has been implemented with festival and loyalty discounts. In the future, if additional discount types are introduced .

the system should allow their inclusion without requiring any modifications to the existing implementation

Example

1: Create an abstraction
2. Create implementations (Extend behavior)
3: Use abstraction in calculator
4: Client code

3.Liskov Substitution principle

We should be to substitute base class objects with child class objects . and this should not alter behavior of program.

Why LSP Is Important
  • Ensures proper use of inheritance
  • Prevents unexpected runtime errors
  • Improves code reliability and maintainability
  • Supports polymorphism

Example : Liskov Substitution Principle with Violation

  1. Rectangle.java

2.Square.java

3. Lspmain.java

when you run the above program its violet the liskov substitution rule . to overcome this solution we will use abstraction using interface .below example solve the above problem .

1.Shape.java

2.Rectangle.java

3.Square.java

4.Interface Segregation principle

clients should not be forced to depend upon interfaces that they do not use .

Interface Pollution : we should not make our interface bigger do not write any unrelated and unused method .

Sign of Interface Segregation violation
  • classes have empty method implementation .
  • method implementations throws unsupported operation Exception .
  • method implementations return null or default/dummy values .

1.Entity.java

2.Order.java

3.User.java

4.PersistenceService.java

  • if you add findByName() method here it will violet the interface segregation principle .
  • if you implement multiple EntitypersistenceService then you have to implement every EntitypersistenceService.
  • you have to remove from persistenceService class and have to implement in particular class .

5.OrderPersistenceService.java

6.UserPersistenceService.java

5.Dependency Inversion principle

High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions.

Example :

Above example tightly couple ,means its depend on the formatter object . creating the object in low level implementation its depend on low level component .

Above code we are instantiating jsonformatter and filewriter object inside method .

  • High level module that means , a module that provides or implement some business rule .
  • A low level module basically that means , a functionality so basic that can be use any where example converting java object to json, it low level module .
  • Out High level module should not depend on low level module .
  • In that case we will create interface and used every where .in above code you will pass different different formatter .
Example of Dependency Inversion Principle Implementation

1. Create formatter abstraction

2.Message.java

3.JsonFormatter.java

4.TextFormatter.java

5.MessagePrinter.java

6.MainTest.java

Leave a Reply

Your email address will not be published. Required fields are marked *