PHP cash Adapter mode

Source: Internet
Author: User
Core idea of PHP Adapter mode: convert operations on some similar classes into a unified "interface" (this is a metaphor)-Adapter, it can also be used as an "interface" to unify or shield the details of those classes. The Adapter mode also constructs a "mechanism" to make the "adaptive" class easy to increase or decrease, while PHP implements the Adapter mode.

Core idea of the adapter mode: convert operations on some similar classes into a unified "interface" (here is a metaphor)-An adapter, or an interface ", unifies or shields the details of those classes. The adapter mode also constructs a "mechanism" to make the "adaptive" class easy to increase or decrease without modifying the code that interacts with the adapter, comply with the design principles of "reducing code coupling.

??? The following example uses the PHP syntax similar to the pseudo code to demonstrate an adapter class for database operations. it can operate on MySQL and Oracle databases, but uses the same method. Because the Adapter mode is used, we do not have to worry about the differences between the MySQL and Oracle database operation classes.

??? We can also easily add the class to database operations such as SQLite. in the "insert" adapter, we can immediately operate the SQLite database using the same method of operating MySQL and Oracle.

??? // Adapter class:
??? // Defines four methods for operating all databases

 Db = $ db_obj;} function select_record () {$ this-> db-> select ();} function insert_record () {$ this-> db-> insert ();} function update_record () {$ this-> db-> update ();} function delete_record () {$ this-> db-> delete ();}} // MySQL database operation class: class Mysql {private $ obj_mysql; function _ construct () {$ obj_mysql = ......;} function select () {$ obj_mysql-> mysql_select ();} function insert () {$ obj_mysql-> mysql_insert ();} funct Ion update () {$ obj_mysql-> mysql_update ();} function delete () {$ obj_mysql-> mysql_delete () ;}// Oracle database operation class: class Oracle {private $ obj_oracle; function _ construct () {$ obj_oracle = ......;} function select () {$ obj_oracle-> oracle_select ();} function insert () {$ obj_oracle-> oracle_insert ();} function update () {$ obj_oracle-> oracle_update ();} function delete () {$ obj_oracle-> oracle_delete () ;}// operate MySQ L database: $ obj = new Db_adapter (new Mysql () $ obj-> select_record (); $ obj-> insert_record (); $ obj-> update_record (); $ obj-> delete_record (); // operate the Oracle Database: $ obj = new Db_adapter (new Oracle () $ obj-> select_record (); $ obj-> insert_record (); $ obj-> update_record (); $ obj-> delete_record ();?>

???? Requirements: MySQL and Oracle classes, with the same name and number of methods. Methods internally implement their own database operation code. The conversion is completed here.
??? Add a new database operation: create a new class with the same name and number of methods. Internal Implementation of the method is not concerned (blocked)-different database implementations are different.
??? Minor disadvantage: the new class may be neglected, resulting in incorrect method names and different numbers.
??? To reduce the possibility of errors, you can improve it. The method is to define an interface that is inherited as a template to achieve standardization.

??? The method in the db_adapter class only needs to use the function in interface db_driver.

 
?

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.