Home  >  Article  >  Backend Development  >  PHP Single Responsibility Principle (SRP) use case analysis

PHP Single Responsibility Principle (SRP) use case analysis

php中世界最好的语言
php中世界最好的语言Original
2018-05-17 10:56:111646browse

This time I will bring you an analysis of PHP Single Responsibility Principle (SRP) use cases. What are the precautions for using PHP Single Responsibility Principle (SRP)? The following is a practical case, let's take a look.

Single Pesponsibility Principle (SRP)

Single responsibility has two meanings: One is to avoid spreading the same responsibilities to different Among the classes, the other one 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, thereby isolating the change; if a class has multiple different responsibilities, they are coupled together, and when one responsibility changes, May interfere with other responsibilities.

(2) Improve the reusability of classes

Modifying a computer is much easier than repairing 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. Taking the data persistence layer as an example, the so-called data persistence layer mainly refers to database operations, and of course, cache management, etc. 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

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

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

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 pass in different generation needs. The class can be

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, you can write like this

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

We separate the create databaseconnection program, so you don’t need to care about the CURD in the program No matter what database it is, just use the corresponding method according to the specifications.

Factory methods free specific objects so that they no longer depend on specific classes, but on abstraction.

The command mode in the design mode is also the embodiment of SRP. The command mode separates the responsibilities of "command requester" and "command implementer". To give a well-understood example, if you go to a restaurant to order a meal, the restaurant has three roles: customer, waiter, and chef. As a customer, you have to list the menu and pass it to the waiter, who then instructs the chef to implement it. As a waiter, you only need to call the method of preparing meals (calling to the chef "It's time to stir-fry"). When the chef hears the request to stir-fry, he will cook immediately. Here, the request and implementation of the command are completely decoupled.

To simulate this process, first define the role of the chef, and the chef will actually do the work of cooking and making soup.

The following is an example

/**
 * 厨师类,命令接受者与执行者
 * 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();
}

It’s the waiter’s turn. The waiter is the transmitter of orders. Usually when you go to a restaurant to eat, you call the waiter. You can’t call the chef directly. Generally, you call the waiter. , bring me a plate of fried tomatoes.” Therefore, the waiter is the communicator of orders between the customer and the chef.

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();
  }
}

Customers can now call the waiter according to the menu

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();
  }
}

好了,现在完成整个过程

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

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

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

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

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

PHP里氏替换案例详解

Bootstrap+PHP实现多图上传步骤详解

The above is the detailed content of PHP Single Responsibility Principle (SRP) use case analysis. 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