|
Posted by Tyno Gendo on 09/28/07 00:04
Hi
I'm trying to learn patterns, which I hope to use in my PHP code,
although I'm finding it hard to get any real impression of how patterns
fit in properly, I've done the following test code for Decorator pattern
and want to know:
a) is it correct, this is decorator pattern?
b) how would i use this in practice with a database, eg. how would i
store the 'attributes' in tables, and how would the 'pattern' be used in
conjunction?
Any hints, tips, pointers to examples would be appreciated:
<?php
interface IProduct {
public function getName();
public function cost();
}
class CProduct implements IProduct {
private $_name = 'Product';
public function getName() {
return $this->_name;
}
public function cost() {
return 10.99;
}
}
class CWhiteProduct implements IProduct {
private $Product = null;
public function __construct( $product ) {
$this->Product = $product;
}
public function getName() {
return 'White ' . $this->Product->getName();
}
public function cost() {
return $this->Product->cost() + 1.50;
}
}
class CLargeProduct implements IProduct {
private $Product = null;
public function __construct ( $product ) {
$this->Product = $product;
}
public function getName() {
return 'Large ' . $this->Product->getName();
}
public function cost() {
return $this->Product->cost() + 3.00;
}
}
$product = new CProduct();
echo "<p>A product is {$product->cost()}</p>";
$largeproduct = new CLargeProduct( $product );
echo "<p>A large product is {$largeproduct->cost()}</p>";
$whiteproduct = new CWhiteProduct( $product );
echo "<p>A white product is {$whiteproduct->cost()}</p>";
$largewhiteproduct = new CLargeProduct( $whiteproduct );
echo "<p>A {$largewhiteproduct->getName()} product is
{$largewhiteproduct->cost()}</p>";
?>
[Back to original message]
|