php原型模式的研究
1 <?php 2 3 class Sea{} 4 class EarthSea extends Sea{} 5 class MarsSea extends Sea{} 6 7 class Plains{} 8 class EarthPlains{} 9 class MarsPlains{} 10 11 class Forest{} 12 class EarthForest{} 13 class MarsForest{} 14 15 16 17 class TerrainFactory{ 18 private $sea; 19 private $forest; 20 private $plains; 21 22 function __construct(Sea $sea,Plains $plains,Forest $forest){ 23 $this->sea = $sea; 24 $this->plains = $plains; 25 $this->forest = $forest; 26 } 27 28 function getSea(){ 29 return clone $this->sea; 30 } 31 function getPlains(){ 32 return clone $this->plains; 33 } 34 function getForest(){ 35 return clone $this->forest; 36 } 37 } 38 39 40 $factory = new TerrainFactory(new EarthSea,new EarthPlains, new EarthForest); 41 print_r($factory->getSea()); 42 print_r($factory->getPlains()); 43 print_r($factory->getForest()); 44 45 $mars_factory = new TerrainFactory(new MarsSea,new MarsPlains, new MarsForest); 46 print_r($mars_factory->getSea()); 47 print_r($mars_factory->getPlains()); 48 print_r($mars_factory->getForest());