17.1. Problem and Solution
The beauty of Object Oriented Programming can sometimes be disfigured by its incorrect usage. Many programmers can grasp the modularization of individual entities as objects. But object-oriented architecture does not stop at just entity architecture. Any task, whether it's the creation of the entity or a modification to said entity, can be encompassed in an object. The Strategy Design Pattern is a great example of taking the object-oriented approach to this next level.
The Strategy Design Pattern details the construction of an object so that it is lighter weight by removing complex logic from itself. Instead of holding a set of logic internally, the object can invoke algorithms from other classes on the fly.
When first examining the Strategy Design Pattern, programmers may mistake the construction of this functionality for standard object inheritance. One of the most common uses of Object Oriented Programming consists of building objects that extend each other. This way, the core object is smaller, with all child objects having additional sets of logic.
Functionally, this can cause a headache. Imagine an object that has multiple children, each of which modify the base object in a specific way. If there is just one single instance of the modification to take place, extending a child class may seem the easiest route to take. The problem comes in when that particular object needs to have multiple modifications. Since the parent/child relationship is already ...
Get Professional PHP Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.