Home arrow PHP arrow Validating Incoming Data with the Strategy Design Pattern

Validating Incoming Data with the Strategy Design Pattern

If you’ve been doing some research lately on the web about how to implement different design patterns, be it in Java, PHP or Python, you'll probably have noticed that the Strategy pattern is one of the most popular contenders in the field. It has been covered countless times in all types of media, ranging from books and magazines to e-zines and personal blogs. In this article series, I'll show you how to use this popular design pattern to accomplish one of the more common tasks a website must perform: validating user-supplied data.

  1. Validating Incoming Data with the Strategy Design Pattern
  2. Building a basic data validation class with PHP
  3. Implementing the Strategy design pattern to validate incoming data
  4. Finish defining the form helper class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
April 14, 2010

print this article



Logically, there’s a reason for the Strategy design pattern's well-deserved popularity: unlike the Composite and Decorator patterns, which are also important to pattern-driven programming, the logic that stands behind Strategy is much easier to grasp, and its implementation in several languages is fairly straightforward as well. While the aforementioned Composite and Decorator are powerful paradigms that permit you to tackle certain software development problems with elegance and efficiency, the truth is that they’re also pretty confusing to understand, at least at first.

On the other hand, the Strategy pattern not only has a flatter learning curve than its cousins, but it allows you to implement two basic principles of object-oriented programming that you’ve probably heard before: “Favor Composition over Inheritance” and “Encapsulate the concept that varies.” Yes, Strategy is a typical case where the power of Composition permits you to create loosely-coupled classes that can be consumed by client code or other classes through a highly-encapsulated interface, without having to create a redundant hierarchy of objects.

With all these goodies at hand for free, it’s logical to think that implementing the Strategy pattern in PHP is a fairly straightforward process. Well, that’s correct, too. And to demonstrate this, in the course of this article series I’ll be showing how to implement the pattern in a real-world case: validating user supplied data. In doing so, you hopefully will learn the basic concepts that surround the application of the Strategy pattern, in addition to building a highly-modular data validation program, which you’ll be able to tweak and improve at will.

Now, it’s time to get rid of the preliminaries and start taking advantage of the functionality offered by the Strategy pattern in PHP. Let’s go!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: