HomePHP Checking Email Addresses with the Strategy Design Pattern
Checking Email Addresses with the Strategy Design Pattern
In this fourth part of a series on the strategy design pattern, I extend the functionality of the sample validation program by adding another strategy class to it. The new class will check the validity of a supplied email address.
As you may have heard, the Strategy design pattern is a software design paradigm that allows developers to build flexible and modular applications by taking advantage of the functionality offered by Composition, rather than relying heavily on the benefits brought by Inheritance. While this may sound like an obscure and hard-to-grasp concept, the truth is that implementing the Strategy pattern is a straightforward process. It's even easier to accomplish when using a programming language as friendly as PHP.
Quite possibly, one of the most challenging aspects that surround the implementation of the Strategy pattern in PHP is to demonstrate how to apply it in a "real-world" use case. Many of the tutorials that cover the topic very often appeal to "toy code examples," which aren't of great help, especially when trying to solve problems present in real environments.
In an attempt to tackle the subject from a realistic point of view, in this article series you'll learn in a step-by-step fashion how to use the Strategy pattern to build a scalable PHP application. This application will be able to check different types of incoming data, including integer and float numbers, email addresses and URLs, without having to use a monolithic God class.
In the last tutorial I left off explaining how to use the power of the Strategy pattern to define an easily-customizable class which validates float numbers in a simple manner. Its loosely-coupled nature makes it possible to use the class as a part of a more complex validation strategy in conjunction with other classes, or as a standalone component.
The primary goal of this series of articles is to develop a flexible and scalable program that can validate supplied data via a set of different strategy classes that can be easily assembled at run time. So, in the following lines I'm going to expand the program's current functionality by adding an entirely new strategy class to it, which will check the validity of a provided email address.
Are you ready to continue learning how to implement the Strategy pattern to validate incoming data? Then begin reading right now!