June 2017
Intermediate to advanced
536 pages
9h 49m
English
The strategy pattern comes in handy where we have multiple chunks of code performing similar operations. It defines an encapsulated and interchangeable family of algorithms. Imagine an order checkout process where we want to implement different shipment providers, such as UPS and FedEx.
The following example demonstrates a possible strategy pattern implementation:
<?phpinterface ShipmentStrategy{ public function calculate($amount);}class UPSShipment implements ShipmentStrategy{ public function calculate($amount) { return 'UPSShipment...'; }}class FedExShipment implements ShipmentStrategy{ public function calculate($amount) { return 'FedExShipment...'; }}class Checkout{ private $amount = 0; public function __construct( ...
Read now
Unlock full access