Home  >  Article  >  Backend Development  >  Detailed explanation of the five object-oriented principles of PHP and the single responsibility principle

Detailed explanation of the five object-oriented principles of PHP and the single responsibility principle

零到壹度
零到壹度Original
2018-04-11 10:52:491862browse

The examples in this article describe the Single Responsibility Principle (SRP) of the five object-oriented principles of PHP. Share it with everyone for your reference. The details are as follows:

Single Pesponsibility Principle (SRP)

Single responsibility has two meanings: One is to avoid the same Responsibilities are dispersed into different classes. The other is to avoid one class taking on too many responsibilities

Why should we comply with SRP?

(1) It can reduce the coupling between classes

If you reduce the coupling between classes, when the requirements change, only one class is modified, thus isolating the change; if a class has multiple different responsibilities, they are coupled together, and when one responsibility changes, it may Interfere with other responsibilities.

(2) Improve the reusability of classes

It is much easier to modify a computer than to repair a TV. The main reason is that the coupling between the various components of the TV is too high, but it is different from the computer. The computer's memory, hard disk, sound card, network card, keyboard light and other components can be easily disassembled and assembled separately. If a part is broken, just replace it with a new one. The above example demonstrates the advantages of single responsibility. Due to the use of single responsibility, 'components' can be easily 'disassembled' and 'assembled'.

Failure to comply with SRP will affect the reusability of classes. When you only need to use a certain responsibility of the class, it is difficult to separate because it is coupled with other responsibilities.

Does complying with SRP have any application in actual code development? some. Take the data persistence layer as an example. The so-called data persistence layer mainly refers to database operations, and of course, cache management. At this time, the data persistence layer needs to support multiple databases. What should be done? Define multiple database operation classes? The idea is already very close. The next step is to use the factory pattern.

Factory pattern (Faction) allows you to instantiate objects when the code is executed. It is called Factory Pattern because it is responsible for ‘producing objects’. Taking the database as an example, what the factory needs is to generate different instantiated objects based on different parameters. The simplest factory is to instantiate an object based on the type name passed in. If it is passed in to MySQL, it calls the MySQL class and instantiates it. If it is SQLite, it calls the SQLite class and instantiates it. It can even handle TXT, Execl, etc.' class database'.

The factory class is such a class, it is only responsible for producing objects, but not the specific content of the objects.

The following is an example

Define an adapter interface

  1. interface Db_Adpater
    {
      /**
       * 数据库连接
       * @param $config 数据库配置
       * @return mixed resource
       */
      public function connect($config);
      /**
       * 执行数据库查询
       * @param $query 数据库查询的SQL字符串
       * @param $handle 连接对象
       * @return mixed
       */
      public function query($query,$handle);
    }

Define a MySQL database operation class that implements the DB_Adpater interface

  1. class Db_Adapter_Mysql implements Db_Adpater
    {
      private $_dbLink;  //数据库连接字符串标识
      /**
       * 数据库连接函数
       * @param $config 数据库配置
       * @return resource
       * @throws Db_Exception
       */
      public function connect($config)
      {
        if($this->_dbLink = @mysql_connect($config->host . (empty($config->port) ? '' : ':' . $config->prot) ,$config->user, $config->password, true))
        {
          if(@mysql_select_db($config->database, $this->_dbLink))
          {
            if($config->charset)
            {
              mysql_query("SET NAME '{$config->charset}'", $this->_dbLink);
            }
            return $this->_dbLink;
          }
        }
        throw new Db_Exception(@mysql_error($this->_dbLink));
      }
      /**
       * 执行数据库查询
       * @param $query 数据库查询SQL字符串
       * @param $handle 连接对象
       * @return resource
       */
      public function query($query,$handle)
      {
        if($resource = @mysql_query($query,$handle))
          return $resource;
      }
    }

Define a SQLite database operation class that implements the DB_Adpater interface

  1. class Db_Adapter_sqlite implements Db_Adpater
    {
      private $_dbLink;  //数据库连接字符串标识
      public function connect($config)
      {
        if($this->_dbLink = sqlite_open($config->file, 0666, $error))
        {
          return $this->_dbLink;
        }
        throw new Db_Exception($error);
      }
      public function query($query, $handle)
      {
        if($resource = @sqlite_query($query,$handle))
        {
          return $resource;
        }
      }
    }

Now If you need a database operation method, you only need to define a factory class and generate the required classes according to the different inputs.

  1. class sqlFactory
    {
      public static function factory($type)
      {
        if(include_once 'Drivers/' . $type . '.php')
        {
          $classname = 'Db_Adapter_'.$type;
          return new $classname;
        }
        else
          throw new Exception('Driver not found');
      }
    }

When called, Just write like this

  1. $db = sqlFactory::factory('MySQL');
    $db = sqlFactory::factory('SQLite');

我们把创建数据库连接这块程序单独拿出来,程序中的CURD就不用关心什么数据库了,只要按照规范使用对应的方法即可。

工厂方法让具体的对象解脱出来,使其不再依赖具体的类,而是抽象。

设计模式里面的命令模式也是SRP的体现,命令模式分离“命令的请求者”和“命令的实现者”方面的职责。举一个很好理解的例子,就是你去餐馆订餐吃饭,餐馆存在顾客、服务员、厨师三个角色。作为顾客,你要列出菜单,传给服务员,由服务员通知厨师去实现。作为服务员,只需要调用准备饭菜这个方法(对厨师喊“该炒菜了”),厨师听到要炒菜的请求,就立即去做饭。在这里,命令的请求和实现就完成了解耦。

模拟这个过程,首先定义厨师角色,厨师进行实际做饭、烧汤的工作。

以下是示例

  1. /**
     * 厨师类,命令接受者与执行者
     * Class cook
     */
    class cook
    {
      public function meal()
      {
        echo '番茄炒鸡蛋',PHP_EOL;
      }
      public function drink()
      {
        echo '紫菜蛋花汤',PHP_EOL;
      }
      public function ok()
      {
        echo '完毕',PHP_EOL;
      }
    }
    //然后是命令接口
    interface Command
    {
      public function execute();
    }

轮到服务员出场,服务员是命令的传送者,通常你到饭馆吃饭都是叫服务员吧,不能直接叫厨师,一般都是叫“服务员,给我来盘番茄炒西红柿”。所以,服务员是顾客和厨师之间的命令沟通都。

  1. class MealCommand implements Command
    {
      private $cook;
      //绑定命令接受者
      public function __construct(cook $cook)
      {
        $this->cook = $cook;
      }
      public function execute()
      {
        $this->cook->meal();//把消息传给厨师,让厨师做饭,下同
      }
    }
    class DrinkCommand implements Command
    {
      private $cook;
      //绑定命令接受者
      public function __construct(cook $cook)
      {
        $this->cook = $cook;
      }
      public function execute()
      {
        $this->cook->drink();
      }
    }

现在顾客可以按照菜单叫服务员了

  1. class cookControl
    {
      private $mealcommand;
      private $drinkcommand;
      //将命令发送者绑定以命令接收器上面来
      public function addCommand(Command $mealcommand, Command $drinkcommand)
      {
        $this->mealcommand = $mealcommand;
        $this->drinkcommand = $drinkcommand;
      }
      public function callmeal()
      {
        $this->mealcommand->execute();
      }
      public function calldrink()
      {
        $this->drinkcommand->execute();
      }
    }

好了,现在完成整个过程

  1. $control = new cookControl;
    $cook = new cook;
    $mealcommand = new MealCommand($cook);
    $drinkcommand = new DrinkCommand($cook);
    $control->addCommand($mealcommand,$drinkcommand);
    $control->callmeal();
    $control->calldrink();

从上面的例子可以看出,原来设计模式并非纯理论的东西,而是来源于实际生活,就连普通的餐馆老板都懂设计模式这门看似高深的学问。其实,在经济和管理活动中对流程的优化就是对各种设计模式的摸索和实践。所以,设计模式并非计算机编程中的专利。事实上,设计模式的起源并不是计算机,而是源于建筑学。

在设计模式方面,不仅以上这两种体现了SRP,还有别的(比如代理模式)也体现了SRP。SRP不只是对类设计有意义,对以模块、子系统为单位的系统架构设计同样有意义。

模块、子系统也应该仅有一个引起它变化的原因,如MVC所倡导的各个层之间的相互分离就是SRP在系统总体设计中的应用。

SRP是最简单的原则之一,也是最难做好的原则之一。我们会很自然地将职责连接在一起。找到并且分离这些职责是软件设计需要达到的目的

一些简单的应用遵循的做法如下:

根据业务流程,把业务对象提炼出来。如果业务的流程的链路太复杂,就把这个业务对象分离为多个单一业务对象。当业务链标准化后,对业务对象的内部情况做进一步处理,把第一次标准化视为最高层抽象,第二次视为次高层抽象,以此类推,直到“恰如其分”的设计层次

职责的分类需要注意。有业务职责,还要有脱离业务的抽象职责,从认识业务到抽象算法是一个层层递进的过程。就好比命令模式中的顾客,服务员和厨师的职责,作为老板(即设计师)的你需要规划好各自的职责范围,即要防止越俎代庖,也要防止互相推诿。

The above is the detailed content of Detailed explanation of the five object-oriented principles of PHP and the single responsibility principle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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