// Products
interface Product
{
    public function show();
}
class ProductA implements Product{
    public function show(){
        echo 'Show ProductA'.PHP_EOL;
    }
}
class ProductB implements Product{
    public function show(){
        echo 'Show ProductB';
    }
}

// Factory
class Factory
{
    public static function createProduct(string $type) : Product
    {
        $product = null;
        switch ($type) {
            case 'A':
                $product = new ProductA();
                break;
            case 'B':
                $product = new ProductB();
                break;
            default:
                $product = new ProductA();
        }
        return $product;
    }
}
$productA = Factory::createProduct('A');
$productB = Factory::createProduct('B');
$productA->show();
$productB->show();
最后修改:2020 年 09 月 16 日 12 : 09 PM
如果觉得我的文章对你有用,请随意赞赏