This repository contains Rust implementations of the Strategy and Command design patterns. These patterns are useful for encapsulating algorithms and operations, making them interchangeable and more manageable.
The Strategy pattern is used to define a family of algorithms, encapsulate each one, and make them interchangeable. This pattern lets algorithms vary independently from the clients that use them.
- Strategy Trait: An interface for executing an operation.
- Concrete Strategies: Implementations of the Strategy interface.
- Context: Maintains a reference to a Strategy object.
trait Strategystruct ConcreteStrategyAstruct ConcreteStrategyBstruct Context
Create instances of concrete strategies and pass them to a context. The context delegates the strategy execution.
The Command pattern encapsulates all information needed to perform an action or trigger an event. This encapsulation allows for more complex architectures like queued and logged operations.
- Command Trait: An interface for executing commands.
- Concrete Commands: Specific commands implementing the Command interface.
- Invoker: Holds a command and can invoke its execution.
trait Commandstruct ConcreteCommandAstruct ConcreteCommandBstruct Invoker
Instantiate concrete command objects and pass them to an invoker. The invoker triggers these commands.
Tests are included for both patterns to ensure functionality. Use cargo test to run these tests.
- Clone the repository.
- Navigate to the project directory.
- Run
cargo buildto compile. - Run
cargo testto execute the tests.