工厂方法模式

<?php
//BT种子的接口
interface BT
{
    public function process();
}

//BT种子的下载处理类
class BTdown implements BT
{
    public function process()
    {
        //输出BT种子
        echo '输出BT种子';
    }
}

//BT种子的上传处理类
class BtUpload implements BT
{
    public function process()
    {
        //保存上传的BT种子
        echo '保存上传的BT种子';
    }
}

//生产BT种子处理对象的设备的接口
interface BTcreator
{
    public function createBTObject();
}

//生产BT种子下载处理对象的设备
class BTdownCreator implements BTcreator
{
    public function createBTObject()
    {
        /**
         * 如果不是管理员,送10点积分
         */
        //然后返回BT种子下载处理对象
        return new BTdown();
    }
}

//生产BT种子上传处理对象的设备
class BTUploadCreator implements BTcreator
{
    public function createBTObject()
    {
        /**
         * 如果不是管理员,送10点积分
         */
        //然后返回BT种子下载处理对象
        return new BtUpload();
    }
}

//生产BT种子处理对象的工厂
class BtFactory
{
    //获取电影处理类的对象,参数为类名
    public function getBtObject($name)
    {
        switch ($name) {
            case 'BTdown':
                $creator = new BTdownCreator();
                break;
            case 'BtUpload':
                $creator = new BtUploadCreator();
                break;
        }
        return $creator->createBTObject();
    }
}

//获取bt工厂
$btFactory = new BtFactory();

//BT种子的下载处理
$what = 'BTdown';
$btObject = $btFactory->getBtObject($what);
$btObject->process();

//BT种子的上传处理
$what = 'BtUpload';
$btObject = $btFactory->getBtObject($what);
$btObject->process();
?>
原文地址:https://www.cnblogs.com/jiufen/p/4989664.html