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).
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"
}
]
- 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.
@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;
}
}
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:
- Implementing HashIteration interface.
- Registering the new class as a Spring component.
The HashIterator automatically includes it.
git clone https://github.com/ercansormaz/iterator-pattern.git
mvn clean install
mvn spring-boot:run
http://localhost:8080/hash
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.
You can read a detailed explanation of this project and the Iterator Pattern in the blog post here:
👉 Read the Blog Post
Contributions are welcome! Feel free to fork the repo, submit pull requests or open issues.
This project is licensed under the MIT License.