设计模式是技术面试的时候难免会被问到的一个问题,特别会让你举例说明各种设计模式的使用场景。
使用设计模式可以减轻我们的工作量,优化我们的代码。
设计模式非常的多,这里介绍单例模式,工厂模式,组合模式,策略模式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>