Skip to content

ercansormaz/iterator-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Iterator Pattern Example: Hashing Service

This project demonstrates the Iterator Design Pattern in a Spring Boot application.
The application receives a text input via REST API and returns the hash values generated by multiple algorithms (MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512).


🚀 API Example

Request:

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

Response:

[
  {
    "algorithm": "MD5",
    "hash": "5d41402abc4b2a76b9719d911017c592"
  },
  {
    "algorithm": "SHA-1",
    "hash": "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"
  },
  {
    "algorithm": "SHA-224",
    "hash": "ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193"
  },
  {
    "algorithm": "SHA-256",
    "hash": "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
  },
  {
    "algorithm": "SHA-384",
    "hash": "59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa90125a3c79f90397bdf5f6a13de828684f"
  },
  {
    "algorithm": "SHA-512",
    "hash": "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043"
  }
]

🛠️ Project Structure

  • HashIteration – Interface for all hash algorithm implementations.
  • MD5HashIteration, SHA256HashIteration, etc. – Concrete classes implementing different algorithms.
  • HashIterator – Central iterator class that loops through all HashIteration implementations.
  • HashService – Calls HashIterator and prepares the response.
  • HashController – REST API endpoint.

🔑 Key Class: HashIterator

@Component
@RequiredArgsConstructor
public class HashIterator {

  private final List<HashIteration> hashIterations;

  public Map<HashType, String> hash(String text) {
    Map<HashType, String> result = new EnumMap<>(HashType.class);

    for (HashIteration hashIteration : hashIterations) {
      result.put(hashIteration.getHashType(), hashIteration.hash(text));
    }

    return result;
  }

}

Why is it important?

The HashIterator class embodies the Iterator Pattern by:

  • Hiding the internal structure of the collection (List).
  • Providing sequential access to all elements (hash algorithms).
  • Returning results in a unified and extensible way.

Adding a new algorithm only requires:

  1. Implementing HashIteration interface.
  2. Registering the new class as a Spring component.

The HashIterator automatically includes it.


⚙️ Run the Project

1. Clone this repository:

git clone https://github.com/ercansormaz/iterator-pattern.git

2. Navigate to the project folder and build:

mvn clean install

3. Run the application:

mvn spring-boot:run

4. Access the APIs at:

http://localhost:8080/hash

📖 Design Pattern Insight

This project demonstrates how the Iterator Pattern can be applied in real-world scenarios where multiple strategies or processors need to be executed sequentially. Instead of exposing the internal list of algorithms, HashIterator provides a clean abstraction.


📚 Further Reading

You can read a detailed explanation of this project and the Iterator 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 Iterator Pattern

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages