欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP面试题之设计模式,php试题设计模式

程序员文章站 2022-05-23 23:13:19
...

PHP面试题之设计模式,php试题设计模式

设计模式是技术面试的时候难免会被问到的一个问题,特别会让你举例说明各种设计模式的使用场景。

使用设计模式可以减轻我们的工作量,优化我们的代码。

设计模式非常的多,这里介绍单例模式,工厂模式,组合模式,策略模式4种模式

/**
 * 单例模式
 * 
 */
class Config
{
    static private $_instance = NULL;
    private $_settings = array();
    private function __construct(){}
    private function __clone(){};

    static function getInstance()
    {
        if(self::$_intance == NULL){
            self::$_intance = new Config();
        }
        return self:$_intance;
    }

    public function set($index,$value)
    {
        $this->_setting[$index] = $value;
    }

    public function get($index)
    {
        return $this->_settings[$index];
    }
}