PHP Redemption Adapter (Adapter) mode

Source: Internet
Author: User
Tags sqlite database
PHP Implementation Adapter (Adapter) mode

Adapter Mode Core idea: the operation of certain similar classes into a unified "interface" (here is the analogy of speaking)-adapter, or analogy to an "interface", unify or block the details of those classes. The adapter pattern also constructs a "mechanism" so that the "fit" class can be easily added and subtracted without modifying the code that interacts with the adapter, in line with the design principle of "reduce inter-code coupling".

??? In the following example, the PHP syntax, which is near the pseudo-code, demonstrates an adapter class for database operations that can manipulate MySQL and Oracle databases, but uses the same method. Due to the use of the adapter (Adapter) mode, we don't have to worry about the differences between MySQL and Oracle database operations classes.

??? We can also easily add to the database operation such as SQLite class, "Insert" adapter, immediately can operate MySQL and Oracle the same way to operate the SQLite database.

??? Adapter class:
??? Defines 4 ways to manipulate 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 ();} function Update () {$obj _mysql->mysql_update ();} function Delete () {$obj _mysql->mysql_delete ();}} Oracle Database Operations 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 ();}} Operation MySQL Database: $obj = new Db_adapter (new MySQL ()) $obj->select_record (); $obj->insert_record (); $obj->update_r Ecord (); $obj->delEte_record (); Operation of Oracle database: $obj = new Db_adapter (new Oracle ()) $obj->select_record (); $obj->insert_record (); $obj->update _record (); $obj->delete_record ();? >

???? Requirements: MySQL, Oracle class, with the same name, the same number of methods. The code of the respective operation database is implemented inside the method. The conversion is done here.
??? Add new Database operations: Constructs a new class with the same name and the same number of methods. Method internal implementations are not cared for (masked)-Different database implementations are different.
??? Small disadvantage: The new class, may be due to negligence, resulting in the method name is not required, the number of different.
??? In order to reduce the possibility of error, can be improved. The method is to define an interface, as a template to inherit, to achieve normalization.

??? In the Db_adapter class, you only need to use the interface Db_driver function.

 
  
?
  • 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.