1. General outline:
1. The five principles of object-oriented: single responsibility principle, interface isolation principle, open-closed principle, replacement principle, and dependency inversion principle.
2. Single responsibility principle:
1. As far as a class is concerned, there is only one reason for its change: the single responsibility principle.
2. Single responsibility has two meanings:
a. Avoid spreading the same responsibilities to different classes
b. Avoid one class taking on too many responsibilities
3. Reasons for following the single responsibility principle: Reduce coupling between classes, Improve class reusability.
Three. Factory pattern:
1. The factory pattern allows objects to be instantiated when code is executed. Able to 'produce' objects.
2. Example:
<?php /* * 单一职责原则 */ interface Db_Adapter{ /* * 数据库连接 * @param $config数据库配置 * @return resource */ public function connect($config); /* * 执行数据库查询 * @param string $query 数据库查询sql字符串 * @param mixed $handld 连接对象 * @return resource */ public function query($query,$handle); } class Db_Adapter_Mysql implements Db_Adapter{ private $_dbLink; /* * 数据库连接 * * @param $config 数据库配置 * @throws Db_Exception * @return resource */ public function connect($config){ if ($this->_dbLink = @mysql_connect($config->host.(empty($config->port) ? '' : ':'.$config->port), $config->user,$config->password,true)){ if (@mysql_select_db($config->database,$this->_dbLink)){ if ( $config->charset){ mysql_query("SET NAMES '{$config->charset}'",$this->_dbLink); } return $this->_dbLink; } } /*数据库异常*/ throw new Db_Exception(@mysql_error($this->_dbLink)); //这一句报了很多错 } /* * 执行数据库查询 * * @param string $query 数据库查询sql字符串 * @param mixed $handle 连接对象 * @return resource */ public function query($query,$handle){ $resource = ""; if ($resource == @mysql_query($query,$handle)){ return $resource; } } } class Db_Adapter_sqlite implements Db_Adapter{ private $_dbLink; //数据库连接字段标记 /* * 数据库连接函数 * * @param $config数据库配置 * @throws DB_exception * @return resource */ public function connect($config){ if ($this->_dbLink = @mysql_connect($config->host. (empty($config->port) ? '' : ':'.$config->port), $config->user,$config->password,true)){ if (@mysql_select_db($config->database, $this->_dbLink)){ if ($config->charset){ mysql_query("SET NAMES '{$config->charset}'",$this->_dbLink); } return $this->_dbLink; } } /*数据库异常*/ throw new Db_exception(@mysql_error($this->dbLink)); } /* * 执行数据库查询 * * @param string $query 数据库查询sql字符串 * @param mixed $handle 连接对象 * @return resource */ public function query($query,$handle){ $resource = ""; if ($resource == @mysql_query($query,$handle)){ return $resource; } } } $testDb = new Db_Adapter_Mysql(); $config = array( //这里写数据库配置 'host'=>'localhost', ); $testDb->connect($config); $testDb->query($sql,$handle);
Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.
The above has introduced (5) Object-oriented design principle 1---the general outline and the single responsibility principle, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.