Design Patterns: Solving Web Development Problems
Are you not sure how to structure your code? Have you come across a problem that makes life difficult. Odds are many developers have been in your situation before. Design patterns are the antacid of the development world…finally relief.

Someone has already solved your problems
You’ve heard of reusing another developer’s code. So why not reuse another developer’s experience. That’s exactly what design patterns try and accomplish. It sort of a guide that stears developers in a direction that has been tested and works. These design patterns make use of what the code Gods deemed as good design principles. It makes your life easier not having to worry as much about your overal code structure.
The great thing about design patterns is, it makes talking about code a whole lot easier. If you ever tried to explain your code to someone you know it can take some time. With design patterns you can just say I used a such and such pattern, and if the developer knows that pattern you’re all on the same page. Get to know them, and it opens a lot of doors.

The Strategy Pattern
This is a less complicated pattern that is used in quite a few other design patterns. It uses the design principle: Identify the aspects of your application that vary and seperate them from the ones that stays the same. This is called encapsulation. Here is an article that might help you find the parts that may change in your code.
Say you have some code like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <?php interface IVehicle { public function revEngine(); } class Car implements IVehicle{ public function revEngine(){ echo "VROOM"; } } class Van implements IVehicle { public function revEngine(){ echo "Putt putt"; } } class Bike implements IVehicle { public function revEngine(){ //Bikes don't have engines } } $car = new Car(); $van = new Van(); $bike = new Bike(); $car->revEngine(); $van->revEngine(); $bike->revEngine(); ?> |
The problem with this example is it isn’t encapsulating what is changing. As you can see in some instances revEngine just doesn’t work. The revEngine varys so we should seperate it out. This will allow us to look after those instances where revEngine doesn’t make sense.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | <?php //Setup Vehicle class Vehicle { private $engine; public function __construct(IEngine $engine) { $this->engine = $engine; } //We'll use rev instead of revEngine. It's more generic. public function rev(){ $this->engine->rev(); } public function setEngine ($engine){ $this->engine = $engine; } } //Setup different Engine Behaviors interface IEngine { public function rev(); } class BigRev implements IEngine{ public function rev(){ echo"VROOM"; } } class LittleRev implements IEngine{ public function rev(){ echo "putt putt"; } } class Pedal implements IEngine{ public function rev(){ echo "silent but speedy"; } } //Setup Your Vehicles class Car extends Vehicle { //Add any code that applies to a car. } class Van extends Vehicle { //Add any code that applies to a van. } class Bike extends Vehicle { //Add any code that applies to a bike. } //Output $car = new Car(new BigRev()); $car->rev(); //VROOM $van = new Van(new LittleRev()); $van->rev(); //putt putt $bike = new Bike(new Pedal()); $bike->rev(); //silent but Speedy //Say you added a rocket to your bike...no prob just change the engine type. $bike->setEngine(new BigRev()); $bike->rev(); ?> |
Overall it looks like a lot more code, but this allows for you to change things about with little trouble. You’re creating little families of code that you can interchange with one another. All in all, very useful.
Related Links
Wikipedia on Strategy Patterns
Design Patterns
Design patterns with C# and .NET Examples
Why Design Patterns are useful

