<?php
/**
* 原型模式
* prototype
* 原型模式是工厂模式的变种,它通过clone进行,而不必为每个子类进行编码
*/
/**
* 创建海洋
*/
class sea
{
}
class earthSea extends sea
{
}
class marsSea extends sea
{
}
/**
* 创建陆地
*/
class plains
{
}
class earthPlains extends plains
{
}
class marsPlains extends plains
{
}
/**
* 创建森林
*/
class forest
{
}
class earthForest extends forest
{
}
class marsForest extends forest
{
}
class terrainFactory
{
private $sea;
private $plains;
private $forest;
public function __construct(sea $sea, plains $plains, forest $forest)
{
$this->sea = $sea;
$this->plains = $plains;
$this->forest = $forest;
}
public function getSea()
{
return clone $this->sea;
}
public function getPlains()
{
return clone $this->plains;
}
public 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());