Quantcast
Channel: The Code Collective » design patterns
Viewing all articles
Browse latest Browse all 7

Prototype Pattern

$
0
0
<?php class Cone{} class PlainCone extends Cone{} class WaffleCone extends Cone{} class IceCream{} class VanillaIceCream extends IceCream{ private $brand = 'Ben & Jerry\'s';//For something to look at in the clone } class ChocolateIceCream extends IceCream{} class Topping{} class Sprinkles extends Topping{} class Nuts extends Topping{} class DesertFactory{ private $_cone,$_ice_cream,$_topping; public function __construct(Cone $cone,IceCream $ice_cream,Topping $topping){ $this->_cone = $cone; $this->_ice_cream = $ice_cream; $this->_topping = $topping; } public function get_cone(){ return clone $this->_cone; } public function get_ice_cream(){ return clone $this->_ice_cream; } public function get_topping(){ return clone $this->_topping; } } echo '~~~~~~~~~~~~~ Prototype Example: ~~~~~~~~~~~~~'; $treat = new DesertFactory(new WaffleCone(),new VanillaIceCream(),new Sprinkles()); echo '<pre>'; print_r($treat); echo '</pre><br><br>'; echo '<pre>'; print_r($treat->get_ice_cream());//Calling get_ice_cream() creates a clone echo '</pre><br><br>'; ?>

Viewing all articles
Browse latest Browse all 7

Trending Articles