简单工厂模式
class Bicycle
{
public function driveTo($destionation)
{
echo '骑车到 '.$destionation.PHP_EOL;
}
}
class SimpleFactory
{
public function createBicycle()
{
$bicycle = new Bicycle();
return $bicycle;
}
}
$factory = new SimpleFactory();
$bicycle = $factory->createBicycle();
$bicycle->driveTo('北京'); // 骑车到 北京
抽象工厂模式
class Bicycle
{
public function driveTo($destionation)
{
echo '骑车到 '.$destionation.PHP_EOL;
}
}
abstract class Factory
{
abstract public function createVehicle();
}
class BicycleFactory extends Factory
{
public function createVehicle()
{
$bicycle = new Bicycle();
return $bicycle;
}
}
$factory = new BicycleFactory();
$bicycle = $factory->createVehicle();
$bicycle->driveTo('巴黎'); // 骑车到 巴黎
网友评论