Skip to content

Spring Boot project demonstrating the Command Pattern

License

ercansormaz/command-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔐 Spring Boot Command Pattern – Crypto Encoder/Decoder

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.


🧩 Overview

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.


🏗️ Architecture

🔸 Core Interfaces & Classes

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.).

🎯 Design Goals

  • 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.

⚙️ API Endpoints

1️⃣ Single Command Execution

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"
}

2️⃣ Batch Command Execution

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"
  }
]

3️⃣ Pipelined Command Execution

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"
  }
]

🧠 Design Insights

  • 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-case logic by using a lookup map initialized via Spring context.

🛠️ How to Run

1. Clone this repository:

git clone https://github.com/ercansormaz/command-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/

🧩 Extending the Project

To add a new crypto method:

  1. Create a new *Cipher component.
  2. Implement a new CryptoCommand class.
  3. Implement a new CryptoCommandFactory for it.
  4. The new command will be auto-registered in the invoker map at startup — no code changes required.

📚 Further Reading

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages