Home > php教程 > php手册 > PHP面试题之设计模式,php试题设计模式

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

WBOY
Release: 2016-06-13 08:53:23
Original
1598 people have browsed it

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

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

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

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

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

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

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

    </span><span>public</span> function <span>get</span><span>($index)
    {
        </span><span>return</span> $<span>this</span>-><span>_settings[$index];
    }
}</span>
Copy after login

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template