zoukankan      html  css  js  c++  java
  • 方法工厂模式

    方法工厂模式

    <?php
    interface LogInterface
    {
        public function log();
    }
    //mysql报错日志
    class MysqlLog implements LogInterface
    {
        public function log()
        {
            // TODO: Implement log() method.
        }
    }
    //Redis报错日志
    class RedisLog implements LogInterface
    {
        public function log()
        {
            // TODO: Implement log() method.
        }
    }
    //用户操作错误日志
    class UserLog implements LogInterface
    {
        public function log()
        {
            // TODO: Implement log() method.
        }
    }
    //代码错诶日志
    class ErrorLog implements LogInterface
    {
        public function log()
        {
            // TODO: Implement log() method.
        }
    }
    
    interface LogFactory
    {
        public function make();
    }
    
    class MysqlLogFactory implements LogFactory
    {
        public function make()
        {
            return new MysqlLog();
        }
    }
    class RedisLogFactory implements LogFactory
    {
        public function make()
        {
            return new RedisLog();
        }
    }
    class UserLogFactory implements LogFactory
    {
        public function make()
        {
            return new UserLog();
        }
    }
    
    class ErrorLogFactory implements LogFactory
    {
        public function make()
        {
            return new ErrorLog();
        }
    }
    
    class Product
    {
        protected $Log;
    
        public function __construct()
        {
            $this->Log = array(
                (new MysqlLogFactory())->make(),
                (new RedisLogFactory())->make(),
                (new UserLogFactory())->make(),
                (new ErrorLogFactory())->make()
            );
        }
    
        public function getLog()
        {
            return $this->Log;
        }
    }
    
    
    
    $product = new Product();
    var_dump($product->getLog());
     
  • 相关阅读:
    SQL——UPDATE(改)
    SQL——INSERT INTO(增)
    SQL——SELECT(查)
    Python——raise引发异常
    Python——异常处理
    Python——多态、检查类型
    Python——继承
    Python——封装
    popitem()方法
    pop(D)方法
  • 原文地址:https://www.cnblogs.com/clubs/p/15169203.html
Copyright © 2011-2022 走看看