This project demonstrates the Command Design Pattern in a clean, extensible way using Spring Boot.
It provides a unified structure to perform encoding and decoding operations with multiple cryptographic methods such as Caesar, Atbash, and ROT13.
Each cryptographic method is implemented as a dedicated *Cipher Spring component.
For each encoding/decoding operation, a corresponding CryptoCommand implementation is created — encapsulating the operation logic, parameters, and execution behavior.
To decouple the command creation logic, each command has its own Factory class that is automatically discovered as a Spring Bean.
At startup, the application builds a lookup map that associates each (CryptoMethod, CryptoOperation) pair with the right CryptoCommandFactory.
| Component | Responsibility |
|---|---|
CryptoCommand |
Command interface defining the execute() method. |
CaesarEncodeCryptoCommand, AtbashCryptoCommand, etc. |
Concrete command implementations encapsulating execution logic. |
CryptoCommandFactory |
Factory interface for creating command instances with input parameters. |
CryptoCommandInvoker |
Central component that locates and executes the right command dynamically. |
CryptoCommandInput |
DTO holding runtime parameters (text, shift, etc.). |
- Decouple command execution logic from request handling.
- Allow easy addition of new crypto methods without modifying existing code.
- Demonstrate extensibility via Factory + Invoker integration within Spring context.
Executes a single encoding/decoding operation.
Request
curl --location 'http://localhost:8080/crypto' \
--header 'Content-Type: application/json' \
--data '{
"operation": "encode",
"method": "caesar",
"text": "hello",
"shift": 3
}'Response
{
"operation": "ENCODE",
"method": "CAESAR",
"shift": 3,
"text": "hello",
"result": "khoor"
}Executes multiple independent commands in a single request.
Request
curl --location 'http://localhost:8080/crypto/batch' \
--header 'Content-Type: application/json' \
--data '{
"commands": [
{"operation":"encode", "method":"caesar", "text":"hello", "shift":3},
{"operation":"encode", "method":"rot13", "text":"hello"},
{"operation":"encode", "method":"atbash", "text":"hello"}
]
}'Response
[
{
"operation": "ENCODE",
"method": "CAESAR",
"shift": 3,
"text": "hello",
"result": "khoor"
},
{
"operation": "ENCODE",
"method": "ROT13",
"text": "hello",
"result": "uryyb"
},
{
"operation": "ENCODE",
"method": "ATBASH",
"text": "hello",
"result": "svool"
}
]Executes a chain of commands where each command’s output becomes the next command’s input. This demonstrates Composite Command behavior — a practical extension of the Command Pattern.
Request
curl --location 'http://localhost:8080/crypto/pipeline' \
--header 'Content-Type: application/json' \
--data '{
"text": "hello world",
"commands": [
{"operation":"encode", "method":"caesar", "shift":3},
{"operation":"encode", "method":"rot13"},
{"operation":"encode", "method":"atbash"}
]
}'Response
[
{
"operation": "ENCODE",
"method": "CAESAR",
"shift": 3,
"text": "hello world",
"result": "khoor zruog"
},
{
"operation": "ENCODE",
"method": "ROT13",
"text": "khoor zruog",
"result": "xubbe mehbt"
},
{
"operation": "ENCODE",
"method": "ATBASH",
"text": "xubbe mehbt",
"result": "cfyyv nvsyg"
}
]- The Command Pattern encapsulates each crypto operation as an independent, executable object.
- The Factory Pattern abstracts command creation, enabling easy extension for new algorithms.
- The Invoker (CryptoCommandInvoker) orchestrates command selection and execution dynamically.
- The Pipeline Mode showcases a Composite Command Pattern — chaining multiple commands.
- The implementation avoids
switch-caselogic by using a lookup map initialized via Spring context.
git clone https://github.com/ercansormaz/command-pattern.gitmvn clean installmvn spring-boot:runhttp://localhost:8080/To add a new crypto method:
- Create a new
*Ciphercomponent. - Implement a new
CryptoCommandclass. - Implement a new
CryptoCommandFactoryfor it. - The new command will be auto-registered in the invoker map at startup — no code changes required.
You can read a detailed explanation of this project and the Command 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.