From the course: PHP: Design Patterns

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Apply the strategy pattern

Solution: Apply the strategy pattern - PHP Tutorial

From the course: PHP: Design Patterns

Start my 1-month free trial

Solution: Apply the strategy pattern

- This solution comes in three parts, listed in the strategies.php file. We have three classes. First, we have a number filter. It takes a single strategy as an input, but the interesting thing about this class is that is doesn't do any work at all. It simply takes the strategy as the input and then calls the strategy filter. Instead, the bulk of the work is done in the other two classes. The even strategy and odd strategy class. In each of these we use the module O operator to check to see if a number is even or odd. In the even strategy class, we only print the even numbers, while in the odd strategy, we only print the odd. There are other ways to approach this math, but this is, by far, the simplest.

Contents