PHP design mode Adapter Pattern code example _php Tutorial

Source: Internet
Author: User

Example of adapter pattern code for PHP design mode


This article mainly introduced the PHP design mode adapter Pattern code example, this article explains the goal, the role, the application scenario, the superiority and so on, and gives the code example, the need friend can refer to the next

Goal:

The interface of one class can be converted into another interface that the customer wants, so that the incompatible interface can work together. The popular understanding is to adapt the different interfaces into a unified API interface.

Role:

Target, which defines which interface the other classes are converted to, which is our desired interface.

Adaptee is the interface that needs to be adapted.

Adapter adapter, the other two roles are already existing roles, and the adapter role needs to be newly established, it is used to adapt the Adaptee and target interface.

Application Scenarios:

If the data operation has MySQL, Mysqli, PDO, SQLite, PostgreSQL, etc., if the build environment needs to replace the database, you can use the adapter mode unified interface. The same is true of the cache scenario, which is more convenient to replace the caching strategy (Memcache, Redis, APC).

Advantage:

The adapter is used by the adaptor to complete the adaptation target to achieve the purpose of transparent to the customer.

Example code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

Adaptation target, the specified interface will be implemented by the matching object

Interface Idatabase

{

Public function Connect ($host, $username, $password, $database);

Public Function query ($sql);

}

Adapter

Class Mysql implements Idatabase

{

protected $connect;

Public function Connect ($host, $username, $password, $database)

{

$connect = mysql_connect ($host, $username, $password);

mysql_select_db ($database, $connect);

$this->connect = $connect;

//...

}

Public Function query ($sql)

{

//...

}

}

Adapter

Class Postgresql implements Idatabase

{

protected $connect;

Public function Connect ($host, $username, $password, $database)

{

$this->connect = Pg_connect ("host= $host dbname= $database user= $username password= $password");

//...

}

Public Function query ($sql)

{

//...

}

}

Client uses

$client = new Postgresql ();

$client->query ($sql);

Above

Target adapter: Idatabase interface

Adaptee: Database manipulation functions for MySQL and PostgreSQL

Adapter adapters: MySQL class and PostgreSQL class

http://www.bkjia.com/PHPjc/998357.html www.bkjia.com true http://www.bkjia.com/PHPjc/998357.html techarticle PHP design mode Adapter Pattern code example this article mainly introduces the PHP design mode adapter Pattern code example, this article explains the goal, the role, the application scenario, the superiority and so on ...

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