/**
* Interface AbstractFactory 抽象工场模式
*/
//抽象工场
interface AbstractFactory{
//创建等级结构工场方法
public function createProductA();
public function createProductB();
}
//具体工场1
class ConcreteFactory1 implements AbstractFactory {
public function createProductA()
{
// TODO: Implement createProductA() method.
return new ProductA1();
}
public function createProductB()
{
// TODO: Implement createProductB() method.
return new ProductB1();
}
}
//具体工场2
class ConcreteFactory2 implements AbstractFactory {
public function createProductA()
{
// TODO: Implement createProductA() method.
return new ProductA2();
}
public function createProductB()
{
// TODO: Implement createProductB() method.
return new ProductB2();
}
}
//抽象产品A
interface AbstractProductA{
public function price();
}
//抽象产品B
interface AbstractProductB{
public function price();
}
//具体产品A1
class ProductA1 implements AbstractProductA{
public function price()
{
// TODO: Implement price() method.
echo ‘ProductA1 价格‘;
}
}
//具体产品A2
class ProductA2 implements AbstractProductA{
public function price()
{
// TODO: Implement price() method.
echo ‘ProductA2 价格‘;
}
}
//具体产品B1
class ProductB1 implements AbstractProductB{
public function price()
{
// TODO: Implement price() method.
echo ‘ProductB1 价格‘;
}
}
//具体产品B1
class ProductB2 implements AbstractProductB{
public function price()
{
// TODO: Implement price() method.
echo ‘ProductB2 价格‘;
}
}
class Client {
public static function main() {
self::run(new ConcreteFactory1());
self::run(new ConcreteFactory2());
}
public static function run(AbstractFactory $factory) {
$productA = $factory->createProductA();
$productB = $factory->createProductB();
echo $productA->price(),PHP_EOL;
echo $productB->price(),PHP_EOL;
}
}
Client::main();
PHP抽象工场模式