https://github.com/nicolalazzaro/strategy-pattern-example
A simple example of the Strategy Pattern, demonstrating how to encapsulate different arithmetic operations.
https://github.com/nicolalazzaro/strategy-pattern-example
example php strategy-pattern
Last synced: 11 months ago
JSON representation
A simple example of the Strategy Pattern, demonstrating how to encapsulate different arithmetic operations.
- Host: GitHub
- URL: https://github.com/nicolalazzaro/strategy-pattern-example
- Owner: nicolalazzaro
- License: unlicense
- Created: 2025-03-08T17:53:46.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-03-08T17:55:57.000Z (11 months ago)
- Last Synced: 2025-03-08T18:30:23.811Z (11 months ago)
- Topics: example, php, strategy-pattern
- Language: PHP
- Homepage:
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Strategy pattern
Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.
[Learn more here](https://refactoring.guru/design-patterns/strategy).
# Considerations
This example demonstrates the **Strategy Pattern**, encapsulating arithmetic operations such as addition, subtraction, and multiplication within separate strategies.
In a real-world scenario, this approach can be used to delegate tasks like calculating the shipping cost of an order. For example, a seller might choose between different pricing strategies, such as a **per-unit cost** or a **fixed fee**. When a new pricing model is needed, a new strategy can be implemented while maintaining a consistent interface, ensuring the codebase remains flexible, organized, and easily extendable.
# How to use
This example can be run using Docker. Build and start the container with the `docker compose up` command, log in and run the `php client.php` command.