您的位置:首页 > 编程语言 > PHP开发

php原型模式的研究

2014-03-20 18:14 513 查看
<?php

class Sea{}
class EarthSea extends Sea{}
class MarsSea extends Sea{}

class Plains{}
class EarthPlains{}
class MarsPlains{}

class Forest{}
class EarthForest{}
class MarsForest{}

class TerrainFactory{
private $sea;
private $forest;
private $plains;

function __construct(Sea $sea,Plains $plains,Forest $forest){
$this->sea = $sea;
$this->plains = $plains;
$this->forest = $forest;
}

function getSea(){
return clone $this->sea;
}
function getPlains(){
return clone $this->plains;
}
function getForest(){
return clone $this->forest;
}
}

$factory = new TerrainFactory(new EarthSea,new EarthPlains, new EarthForest);
print_r($factory->getSea());
print_r($factory->getPlains());
print_r($factory->getForest());

$mars_factory = new TerrainFactory(new MarsSea,new MarsPlains, new MarsForest);
print_r($mars_factory->getSea());
print_r($mars_factory->getPlains());
print_r($mars_factory->getForest());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: