header("content-type:text/html;charset=utf-8");
//---------------------PHP 抽象工厂模式----------------------
interface Factory{
public function A();
public function B();
}
class Product1 implements Factory{
public function A(){
return new A1;
}
public function B(){
return new B1;
}
}
class Product2 implements Factory{
public function A(){
return new A2;
}
public function B(){
return new B2;
}
}
interface ColorA{
public function color();
}
interface ColorB{
public function color();
}
class A1 implements ColorA{
public $color = 'A1的颜色是白色<br>';
public function color(){
return $this->color;
}
}
class A2 implements ColorA{
public $color = 'A2的颜色是黑色<br>';
public function color(){
return $this->color;
}
}
class B1 implements ColorB{
public $color = 'B1的颜色是白色<br>';
public function color(){
return $this->color;
}
}
class B2 implements ColorB{
public $color = 'B2的颜色是黑色<br>';
public function color(){
return $this->color;
}
}
class Client{
public static function main(){
self::run(new Product1);
self::run(new Product2);
}
public static function run(Factory $factory){
$a = $factory->A();
echo $a->color();
$b = $factory->B();
echo $b->color();
}
}
Client::main();
-----------------------------------------
结果:
A1的颜色是白色
B1的颜色是白色
A2的颜色是黑色
B2的颜色是黑色
PHP 抽象工厂模式
最新推荐文章于 2023-07-27 18:08:48 发布