Skip to content

Spring Boot project demonstrating the Chain of Responsibility Pattern

License

ercansormaz/chain-of-responsibility-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Chain of Responsibility Pattern Example: Hash Resolving Service

📘 Overview

This project demonstrates the Chain of Responsibility Pattern in a Spring Boot application.

The goal is to resolve which hashing algorithm (e.g., MD5, SHA1, SHA256, etc.) was used to generate a given hash, based on the provided plain text.

The API accepts a plain text input and a hash value. Each handler in the chain (representing a hash algorithm) checks whether the given text, when hashed using its algorithm, matches the provided hash value.

If a match is found, the API returns the matching algorithm type — otherwise, the next handler takes over.


🧠 Design Pattern: Chain of Responsibility

The Chain of Responsibility pattern is used to decouple senders and receivers of requests.

Each handler in the chain decides either to handle the request or to pass it to the next handler.

In this project:

  • Each handler represents a hash algorithm (e.g., MD5, SHA-1, SHA-256, etc.)
  • The chain continues until a match is found or no algorithms match.
  • The chain is orchestrated by the HashResolveChainOrchestrator, which dynamically builds the handler chain based on each algorithm’s hash length during the application’s startup (@PostConstruct). This means the service layer does not interact with individual handlers directly — all hash checks are performed through the orchestrator.

🧩 Implemented Hash Algorithms

The following algorithms are currently implemented in the chain:

Algorithm Class
MD5 MD5HashResolveChainHandler
SHA-1 SHA1HashResolveChainHandler
SHA-224 SHA224HashResolveChainHandler
SHA-256 SHA256HashResolveChainHandler
SHA-384 SHA384HashResolveChainHandler
SHA-512 SHA512HashResolveChainHandler

💡 You can extend the project by implementing the missing SHA3-224, SHA3-256, SHA3-384 and SHA3-512 handlers. Try creating your own handler classes!


📡 API Endpoint

Example Request

curl --location 'http://localhost:8080/hash/resolve' \
--header 'Content-Type: application/json' \
--data '{
  "text": "hello world",
  "hash": "5eb63bbbe01eeed093cb22bb8f5acdc3"
}'

Example Response

{
  "algorithm": "MD5"
}

🚀 Running the Project

1. Clone this repository:

git clone https://github.com/ercansormaz/chain-of-responsibility-pattern.git
cd chain-of-responsibility-pattern

2. Navigate to the project folder and build:

mvn clean install

3. Run the application:

mvn spring-boot:run

4. Test the endpoint (example above)


📚 Further Reading

You can read a detailed explanation of this project and the Chain of Responsibility Pattern in the blog post here:
👉 Read the Blog Post


🤝 Contributing

Contributions are welcome! Feel free to fork the repo, submit pull requests or open issues.


📜 License

This project is licensed under the MIT License.

About

Spring Boot project demonstrating the Chain of Responsibility Pattern

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages