HomePHP Verifying Float Values with the Strategy Design Pattern
Verifying Float Values with the Strategy Design Pattern
In this third part of a series on validating incoming data with the strategy design pattern, I create a brand new strategy class that can check whether or not a supplied input value is a float number. The addition of this class extends the capabilities of the sample validation program that I’m building in this series.
Design patterns are well-known, reusable paradigms that allow developers to solve certain problems in a more efficient way, even though their implementation is flexible and not tied to a specific programming language. Due to their variety, they can be placed in different categories (creation, structural, behavioral, etc.), but their rather fuzzy boundaries tend to create large gray areas between them.
It’s valid to say, though, that a number of patterns can be of great help, especially when developing more flexible and scalable applications. A good example of this is the triad made up of the Decorator, Composite and Strategy patterns, which share a common feature worth stressing here: all of them tend to favor Composition over Inheritance.
Unlike its handy partners Composite and Decorator, which tackle some common software development problems in an elegant and efficient manner at the expense of a steeper learning curve, Strategy is incredibly easy to implement. This becomes even more evident when using a language as friendly as PHP. When applied in an appropriate case, the Strategy pattern is a powerful tool that permits you to merge two of the big pillars of the object-oriented paradigm: first, to use Composition over Inheritance, and second, to encapsulate the concept that varies.
To demonstrate how useful and easy it is to apply the Strategy pattern in a real-world case using PHP, in the two previous tutorials of this series I started building a modular web application. It was tasked with validating incoming data through a set of loosely-coupled strategy classes. So far, I've shown how to provide this sample program with the ability to check integer values via an isolated “integer strategy” class, but it’s necessary to extend the program’s current functionality by adding a few more validation classes to it. Therefore, in the following lines I’m going to develop a brand new strategy class, which will be charged with checking whether or not a supplied value is a float number.
Are you ready to learn more about this process? Then begin reading right now!