PHP design mode-Adapter mode

Source: Internet
Author: User
The adapter mode (sometimes called packaging style or packaging) adapts the interface of a class to what you expect. An adaptation allows a class that cannot work together because the interface is incompatible.

Introduction

The adapter mode (sometimes called packaging style or packaging) adapts the interface of a class to what you expect. An adaptation allows a class that cannot work together because the interface is incompatible.

UML

Role

Target adaptation Target: This role defines the interface for converting other classes, that is, our expected interface.

Adaptee adapter: the interface to be adapted.

Adapter: The other two roles are existing roles, and the Adapter role needs to be created to adapt the Adaptee to the Target interface.

Application scenarios

For example, if the program database is associated with mysql, mysqli, pdo, sqlite, postgresql, and other operations, and you need to change the database operation as needed, you can use the Adapter mode to unify the interface, in this way, the code does not need to be changed except for the database configuration.

Similarly, the cache scenario is that whether using memcache or redis, it will be convenient and time-saving during the change.

Note: This mode can be seen in some popular frameworks. for details, see the framework source code.

Implementation

Code:

 Connect = $ connect; // Other operations}/*** query method ** @ param $ the SQL statement to be queried * @ return mi */public function query ($ SQL) {return mysql_query ($ SQL);} // implement the close method public function close () {mysql_close ();}} /*** Class Mysql adapter */class Mysql_ I implements IDatabase {protected $ connect; // connect to the resource/*** implement the connection method ** @ param $ host * @ param $ username * @ param $ password * @ param $ database name */public function connect ($ host, $ username, $ password, $ database) {$ connect = mysqli_connect ($ host, $ username, $ password, $ database); $ this-> connect = $ connect; // Other operations}/*** query method ** @ param $ SQL the SQL statement to be queried */public function query ($ SQL) {return mysqli_query ($ this-> connect, $ SQL);} // implement public function close () {mysqli_close ($ this-> connect );}} /*** Class Postgresql Adapter */class Postgresql implements IDatabase {protected $ connect; // connect to the resource/*** implement the connection method ** @ param $ host * @ param $ username * @ param $ password * @ param $ database */public function connect ($ host, $ username, $ password, $ database) {$ this-> connect = pg_connect ("host = $ host dbname = $ database user = $ username password = $ password "); // Other operations}/*** query method ** @ param $ SQL the SQL statement to be queried */public function query ($ SQL) {// Other operations} // implement the close method public function close () {}}/*** client usage demo * here mysql is used as an example * as long as the mode is well designed, regardless of the number of databases, the implementation and call methods are the same * because they are the same interface, they can be switched freely */$ host = 'localhost '; $ username = 'root'; $ password = 'root'; $ database = 'mysql'; // $ client = new Postgresql (); // $ client = new Mysql (); $ client = new Mysql_ I (); $ client-> connect ($ host, $ username, $ password, $ database ); $ result = $ client-> query ("select * from db"); while ($ rows = mysqli_fetch_array ($ result) {var_dump ($ rows );} $ client-> close ();

Running result:

array(44) {  [0]=>  string(1) "%"  ["Host"]=>  string(1) "%"  [1]=>  string(4) "test"  ["Db"]=>  string(4) "test"  [2]=>  string(0) ""  ["User"]=>  string(0) ""  [3]=>  string(1) "Y"  ["Select_priv"]=>  string(1) "Y"  [4]=>  string(1) "Y"  ["Insert_priv"]=>  string(1) "Y"  [5]=>  string(1) "Y"  ["Update_priv"]=>  string(1) "Y"  [6]=>  string(1) "Y"  ["Delete_priv"]=>  string(1) "Y"  [7]=>  string(1) "Y"  ["Create_priv"]=>  string(1) "Y"  [8]=>  string(1) "Y"  ["Drop_priv"]=>  string(1) "Y"  [9]=>  string(1) "N"  ["Grant_priv"]=>  string(1) "N"  [10]=>  string(1) "Y"  ["References_priv"]=>  string(1) "Y"  [11]=>  string(1) "Y"  ["Index_priv"]=>  string(1) "Y"  [12]=>  string(1) "Y"  ["Alter_priv"]=>  string(1) "Y"  [13]=>  string(1) "Y"  ["Create_tmp_table_priv"]=>  string(1) "Y"  [14]=>  string(1) "Y"  ["Lock_tables_priv"]=>  string(1) "Y"  [15]=>  string(1) "Y"  ["Create_view_priv"]=>  string(1) "Y"  [16]=>  string(1) "Y"  ["Show_view_priv"]=>  string(1) "Y"  [17]=>  string(1) "Y"  ["Create_routine_priv"]=>  string(1) "Y"  [18]=>  string(1) "N"  ["Alter_routine_priv"]=>  string(1) "N"  [19]=>  string(1) "N"  ["Execute_priv"]=>  string(1) "N"  [20]=>  string(1) "Y"  ["Event_priv"]=>  string(1) "Y"  [21]=>  string(1) "Y"  ["Trigger_priv"]=>  string(1) "Y"}

From the above results, we can see that the database connection and query statements have been successfully executed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.