打赏

简单工厂模式

简单工厂模式

<?php
//简单工厂模式

//mysql报错日志
class MysqlLog
{
}
//Redis报错日志
class RedisLog
{
}
//用户操作错误日志
class UserLog
{
}
//代码错诶日志
class ErrorLog
{
}

class Product
{
    protected $log;

    public function __construct(array $module)
    {
        $factory = new LogFactory();

        foreach ($module as $item) {
            $this->log[$item] = $factory->make($item);
        }
    }

    public function getLog()
    {
        return $this->log;
    }
}

class LogFactory
{
    public function make($abstract)
    {
        switch ($abstract) {
            case 'mysql':
                return new MysqlLog();
            case 'redis':
                return new RedisLog();
            case 'user':
                return new UserLog();
            case 'error':
                return new ErrorLog();
        }
    }
}

$pra = new Product(array(
    'mysql',
    'redis',
    'user',
    'error'
));
var_dump($pra->getLog());

 

 

posted on 2021-08-21 11:30  头大的冯冯  阅读(12)  评论(0编辑  收藏  举报

导航