Design patterns are often mentioned during the interview process, and sometimes we give examples of scenarios where design patterns are applied.
Using design patterns can reduce our workload and optimize our code.
Design pattern is very much, here is a single example mode, Factory mode, combination mode, strategy mode 4 modes
If there are any problems with the code or if there is a better way please let me know, thank you!!!!!
/** * Single case mode * @author Yangyang <1812271619@qq.com> * Can be thought of as an object that only produces the class in an HTTP request (that is, the new classname only once) * The classic example is a database connection (r Edis,mongodb,memcache etc) * In an HTTP request we may need to do the database additions and deletions to check more than one SQL operation * But if you execute one SQL per HTTP request, we mysql_connect (), It's obviously going to be a waste of server resources. To save resources, you can implement an HTTP request through a single case pattern only once mysql_connect () * mysql_connect () will be placed in the __construct of the class method, and __ The construct method is private, * so that only the getinstance () method can be used to obtain the mysql_connect () resource connector * getinstance () method to determine if a MYQL connector already exists, and if so, to return the connector directly * Otherwise, the new classname () invokes the __construct method to execute the mysql_connect () with the resource connector and returns the connector * because now PHP no longer recommends using MySQL functions directly for database operations. It is recommended to do database operations through PDO, so here is a simple PDO connection to write a single case mode * Here is only to explain the single example principle, database SQL injection and other issues do not consider * Prepare working database: Test data table: User field: ID Name record: 1 Codeanti *
Final Run Result: id=1 This record is deleted in Datasheet user */class Singlepdo {private static $_instance = null;
Private $_pdo;
Private, prevent external direct instantiation of new singlepdo (...) Private Function __construct ($DSN, $dbUser, $dbPassword) {try{$this->_pdo = new PDO ($DSN, $dbUse
R, $dbPassword); $this->_pdo->exec (' sEt names UTF8 ');
}catch (Pdoexception $e) {die ("error:{$e->getmessage ()}"); }//Private, prevent clone private function __clone () {}//Get connection instance public static function getinstance ($DSN, $dbUser, $d
Bpassword) {if (self::$_instance = = null) Self::$_instance = new self ($dsn, $dbUser, $dbPassword);
return self::$_instance;
//Execute SQL Public Function Execsql ($sql) {$result = $this->_pdo->exec ($sql);
return $result;
}} $dsn = "Mysql:host=localhost;dbname=test";
$dbUser = "root";
$dbPassword = "";
$sql = "Delete from user where id = 1";
$pdo = Singlepdo::getinstance ($dsn, $dbUser, $dbPassword); $result = $pdo->execsql ($sql); $pdo->execsql ($sql) is invoked multiple times, but remains the same PDO object Print_r ($result);