PHP 抽象工厂模式(Kit模式)

<?php
/**
 * PHP 抽象工厂模式(Kit模式)
 * 例商品抽象出类别,例如 电脑 电视 抽象成
 * 电脑 computed 接口  电视 TV 接口
 * 商品品牌为具体实现类
 * 工厂类抽象类
 * 具体工厂是负责创建 商品
 */

/**
 * Interface ComputedIO
 * 抽象产品种类
 */
interface ComputedIO {
    public function Name ();
    public function Price ();
}

/**
 * Class DellXps
 * 具体品牌产品
 */
class DellXps implements  ComputedIO {
    public function Name()
    {
        echo 'New Xps13 dell';
        // TODO: Implement Name() method.
    }

    public function Price()
    {
        echo 'Price:6999';
        // TODO: Implement Price() method.
    }
}

/**
 * Interface TvIO
 * 抽象种类接口
 */
interface TvIO {
    public function Brands();
}

/**
 * Class SonyTv
 * 具体品牌产品
 */

class SonyTv implements TvIO {
    public function Brands()
    {
        echo 'Sony-69';
        // TODO: Implement Brands() method.
    }

}

/**
 * Class AbsFactory
 * 工厂抽象
 * 工厂的能力
 */
abstract class AbsFactory {
    abstract public static function NewComputed();
    abstract public static function NewTV();
}

/**
 * Class Factory
 * 工厂能力的实现类
 */

class Factory extends AbsFactory {
    public static function NewComputed()
    {
        return new DellXps();
        // TODO: Implement NewComputed() method.
    }

    public static function NewTV()
    {
        return new SonyTv();
        // TODO: Implement NewTV() method.
    }
}

$dell = Factory::NewComputed();
$dell->Name();
$dell->Price();
// New Xps13 dellPrice:6999

echo ''.PHP_EOL;

$sony = Factory::NewTV();
$sony->Brands();
// Sony-69
echo ''.PHP_EOL;

猜你喜欢

转载自my.oschina.net/u/3529405/blog/1823474