'; ?> DomCore Demos, a WebAbility® Network Project
« Back to the index

\patterns\Factory example

i = (int)$data; } function get() { return $this->i; } } class Real { private $r; function __construct($data = 0) { $this->r = (float)$data; } function get() { return $this->r; } } class Complex { private $cx, $cy; function __construct($x = 0, $y = 0) { $this->cx = (float)$x; $this->cy = (float)$y; } function get() { return '['.$this->cx.','.$this->cy.']'; } } class Point { private $px, $py, $pz; function __construct($x = 0, $y = 0, $z = 0) { $this->px = (float)$x; $this->py = (float)$y; $this->pz = (float)$z; } function get() { return '['.$this->px.','.$this->py.','.$this->pz.']'; } } class myFactory extends \patterns\Factory { function __construct() { parent::__construct(array('i' => 'Integer', 'r' => 'Real', 'c' => 'Complex')); } } print "Our factory build numbers, we have Integer (i), Real (r) and Complex (c) numbers available:
"; print "Lets build one of each.
"; $F = new myFactory(); $I = $F->newProduct('i', 4.6); $R = $F->newProduct('r', 8.6); $C = $F->newProduct('c', 12.6, -4); print "A result with our integer ".$I->get() . "
"; print "A result with our real ".$R->get() . "
"; print "A result with our complex ".$C->get() . "

"; print "Lets add a 4th product: the space point.
"; $F->registerProduct('p', 'Point'); print "Lets build a point.
"; $P = $F->newProduct('p', 2, 3, 4); print "A result with our point ".$P->get() . "
"; ?>



« Back to the index