From the course: PHP: Design Patterns
Unlock the full course today
Join today to access over 22,700 courses taught by industry experts or purchase this course individually.
Exploring a use case for the strategy pattern - PHP Tutorial
From the course: PHP: Design Patterns
Exploring a use case for the strategy pattern
- In the beginning of this one, we only have two files. One file, output.php, with the behaviors we're using and the index.php, which calls them. Unfortunately, to get at the behaviors, we have to call each one individually with its own name. Even worse, if we need to add additional behaviors, we have to modify the output class to add them or extend the class, add our custom behaviors, and now maintain both classes. The strategy base solution is quite a bit different but simpler in a number of ways. Now we create individual classes that implement the behavior we want. Then we pass them into the class at runtime and deal with the output in a single way. We need to add more behaviors. We don't need to modify the existing classes. We just add additional classes. In this case, our Square, Cube and Fourth classes do all the work. We just call them individually by the identical interfaces. A nice side effect of this is that we get to re-use these behaviors all over the place with very…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.