The first chapter of Head First Design Patterns is titled “Intro to design patterns” . Here are some gems I like:
- “The best way to use patterns is to load your brain with them and then recognize places in your designs and existing applications where you can apply them” (p. 1)
- “No matter how well you design an application, over time an application must grow and change or it will die.” (p. 8 )
- “All patterns provide a way to let some part of a system vary independently of all other parts.” (p. 9) This is a great statement and the #1 reason I’m trying to learn these now. Basically, you want to design your code so that the parts that don’t change are separate from the parts that do change. For the parts that do change, they need to be designed in such a way that changing them won’t blow up the rest of your system.
- “Program to an interface, not an implementation” (p. 11) I’ve heard this a thousand times before…this book does a good job explaining what it means. Summarizing this using PHP would give something like:
- “Favor composition over inheritance”. (p. 23) Basically the above is an example of composition, where an object (Duck) in addition to its own properties is composed of other objects (flybehavior, quackbehavior). WOOT, I’ve been doing this for about a year now, and couldn’t agree more…it makes much more since this way.
//Programming to an implementation...bad $spot = new Dog(); $spot->woof(); //Programming to an interface/supertype...good $animal = new Animal(); $animal->speak(); //Programming to an interface and assigning implementation at runtime...best $animal = getAnimal(); $animal->speak();
Strategy Pattern (brief look)
There is a great example about designing your code for expandability w/out code reuse using the Strategy pattern. I translated the java code for the duck/mallard example into PHP, as seen below:
<?php
//Our base duck class
abstract class Duck
{
//Setting these as private forces the offspring to use the setter methods,
//which are the only way to enforce class type on the variables in PHP
private $flyBehavior;
private $quackBehavior;
abstract public function display();
public function performFly()
{
$this->flyBehavior->fly();
}
public function performQuack()
{
$this->quackBehavior->quack();
}
public function swim()
{
echo 'All ducks float, even decoys!<br />';
}
public function setFlyBehavior(flyBehavior $fb)
{
$this->flyBehavior = $fb;
}
public function setQuackBehavior(quackBehavior $qb)
{
$this->quackBehavior = $qb;
}
}
//Mallard is a type of duck
class MallardDuck extends Duck
{
public function __construct()
{
$this->setQuackBehavior(new NormalQuack());
$this->setFlyBehavior(new FlyWithWings());
$this->display();
}
public function display()
{
echo '<hr />I am a mallard duck<br />';
}
}
//Model is a type of duck (a plastic one)
class ModelDuck extends Duck
{
public function __construct()
{
$this->setQuackBehavior(new MuteQuack());
$this->setFlyBehavior(new FlyNoWay());
$this->display();
}
public function display()
{
echo '<hr />I am a model duck<br />';
}
}
interface flyBehavior
{
public function fly();
}
class FlyWithWings implements flyBehavior
{
public function fly()
{
echo "I'm Flying!<br />";
}
}
class FlyNoWay implements flyBehavior
{
public function fly()
{
echo "I can't fly!<br />";
}
}
class FlyRocketPowered implements flyBehavior
{
public function fly()
{
echo "I'm flying with a rocket!<br />";
}
}
interface quackBehavior
{
public function quack();
}
class NormalQuack implements quackBehavior
{
public function quack()
{
echo 'QUACK<br />';
}
}
class MuteQuack implements quackBehavior
{
public function quack()
{
echo "-- Silence --<br />";
}
}
class Squeak implements quackBehavior
{
public function quack()
{
echo 'Squeak<br />';
}
}
//Now run a test
$mallard = new MallardDuck();
$mallard->performQuack();
$mallard->performFly();
//test the model duck
$modelduck = new ModelDuck();
$modelduck->performQuack();
$modelduck->performFly();
I must say I really like this example. Its basic OO principles, which I definitely understood before, but using this kind of model and then implementing it is a very good way to cement the concepts in your brain. The idea of pulling out behaviors into separate classes adds a lot of flexibility and yet doesn’t require the countless lines of duplicate code that will happen if you do this incorrectly.
In translating this I also found a good use of private methods in PHP. Since PHP doesn’t allow typecasting in the variable declarations, you can just make the variables private, and then use a public/protected setter method that forces the variable to be a certain type. I’m going to implement this from now on.
The above is called the “STRATEGY PATTERN”. It “Lets the algorithm vary independently from clients that use it” (p. 24).
Comments are closed, but trackbacks and pingbacks are open.