开闭原则 Open/Closed Principle (OCP)
正如Bertrand Meyer所述,"软件的实体(类, 模块, 函数,等)应当对扩大开放,对修改封闭。"这个原则是在解释应当许可用户在不改变已有代码的情况下增长新的功能。
Bad:
- abstract class Adapter{
- protected $name;
- public function getName(){
- return $this->name;
- }
- }
- class AjaxAdapter extends Adapter{
- public function __construct(){
- parent::__construct();
- $this->name = 'ajaxAdapter';
- }
- }
- class NodeAdapter extends Adapter{
- public function __construct(){
- parent::__construct();
- $this->name = 'nodeAdapter';
- }
- }
- class HttpRequester{
- private $adapter;
- public function __construct($adapter)
- {
- $this->adapter = $adapter;
- }
- public function fetch($url)
- {
- $adapterName = $this->adapter->getName();
- if ($adapterName === 'ajaxAdapter') {
推荐阅读
沙龙晃荡 | 去哪儿、陌陌、ThoughtWorks在主动化运维中的实践!10.28不见不散! 比来在进行微办事架构的交换和>>>详细阅读
本文标题:PHP代码简洁之道——SOLID原则
地址:http://www.17bianji.com/lsqh/38154.html
1/2 1