Skip to content

nicolalazzaro/strategy-pattern-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

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.

About

A simple example of the Strategy Pattern, demonstrating how to encapsulate different arithmetic operations.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages