Posted: May 28th, 2008 | Author: tolleiv | Tags: creational, pattern, prototype | Comments Off
Imagine a cookie-oven which produces tasty cookies with chocolate crumbles. How do you ensure that the 1000th cookie still has the same taste as the first?
You might think that this is an easy task – just write down the recipe and follow the described steps…you know the result in real life – the 1000th cookie normally tasts like the 1st but you always had the “overhead” to read the recipe and go through the steps again and again.
In OOP it’s much easier to follow the recipe just instantiate a new Object and there you go… no matter if it’s the 1st or the 1000th – it’ll always taste look similar.
But the “recipe-overhead” is still there in a way and especially when you have larger objects whose construction is time-consuming you might want to somehow get rid of it. And that’s where a Prototype can help you out – you just create the first Cookie Object and then you use the handy magic method __clone to create new objects.
Instead of just using __clone the pattern suggests a class (some kind of a factory-class) so that you can also encapsulate the creation of the objects (and also possible adjustments you might want to make after the creation/clone).
So the example just shows a cookie-machine which makes use of the prototype-pattern to create new cookies (depending on the cookie you throw in before)… yummy
abstract class Cookie {
function __clone() { }
abstract public function printFlavor();
}
class CoconutCookie extends Cookie {
public function printFlavor() {
echo ‘Coconut Flavor<br/>’;
}
}
class ChocolateCookie extends Cookie {
public function printFlavor() {
echo ‘Chocolate Flavor<br/>’;
}
}
class CookieMachine {
protected $cookie;
public function __construct(Cookie $cookie) {
$this->cookie = $cookie;
}
public function makeCookie() {
return clone $this->cookie;
}
}
The client-code can look like this:
$coconutCookie = new CoconutCookie();
$coconutCookieMachine = new CookieMachine($coconutCookie);
$chocolateCookie = new ChocolateCookie();
$chocolateCookieMachine = new CookieMachine($chocolateCookie);
//while(true) {
for($i=0;$i<5;$i++) {
$coconutCookieMachine->makeCookie()->printFlavor();
$chocolateCookieMachine->makeCookie()->printFlavor();
}
Imagine a cookie-oven which produces tasty cookies with chocolate crumbles. How do you ensure that the 1000th cookie still has the same taste as the first?
You might think that this is an easy task - just write down the recipe and follow the described steps...you know the result in real life - the 1000th ...
Posted: May 13th, 2008 | Author: tolleiv | Tags: pattern, prototype, structural | Comments Off
As the first pattern I’d like to introduce the decorator pattern – it’s one of the [GoF]- structural patterns. It enables to extend the functionality of a existing method by wrapping a so called decorator-object.
So maybe you already know the situation
, your granny is going to bake cookies and you think of how they gonna taste – so cookie is our main-object and the different additional spices and other options which refine the taste of the cookies are the decoration for it. The cookies, pardon main-objects, are fine without the decoration but they’re much better with and the best thing is that you’re able to combine the decorations… yummy
So that’s how this would look like more technically:

stract class Cookie {
protected $flavor;
public function __construct($flavor) {
$this->flavor=$flavor;
}
abstract public function descripeFlavor();
}
class GrannysCookie extends Cookie {
public function descripeFlavor() {
echo ‘
Granny baked a cookie which has a taste of ’;
echo $this->flavor;
}
}
abstract class CookieDecorator extends Cookie {
protected $cookie;
public function __construct(Cookie $cookie) {
$this->cookie = $cookie;
}
//abstract public function descripeFlavor();
}
class FreshCookieDecorator extends CookieDecorator {
public function descripeFlavor() {
$this->cookie->descripeFlavor();
echo ‘ which smells fresh from the oven’;
}
}
class CrumbleCookieDecorator extends CookieDecorator {
public function descripeFlavor() {
$this->cookie->descripeFlavor();
echo ‘ it has tasty crumbles ’;
}
}
$cookie = new GrannysCookie(‘chocolate’);
$cookie->descripeFlavor();
$crumbleCookie = new CrumbleCookieDecorator($cookie);
$crumbleCookie->descripeFlavor();
$freshCookie = new FreshCookieDecorator($cookie);
$freshCookie->descripeFlavor();
$freshAndCrumbledCookie = new FreshCookieDecorator($crumbleCookie);
$freshAndCrumbledCookie->descripeFlavor();
additional Information
As the first pattern I'd like to introduce the decorator pattern - it's one of the - structural patterns. It enables to extend the functionality of a existing method by wrapping a so called decorator-object.
So maybe you already know the situation ;) , your granny is going to bake cookies and you think ...