Design pattern is a question that will inevitably be asked during technical interviews. In particular, it will ask you to give examples of various Design pattern usage scenarios.
Using design patterns can reduce our workload and optimize our code.
There are many design patterns. Here we introduce 4 patterns: singleton pattern, factory pattern, combination pattern and strategy pattern
<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>