PHP design mode-Adapter mode code example _ PHP Tutorial

Source: Internet
Author: User
PHP design mode: Adapter mode code instance. This article describes the sample code of the adapter mode in PHP design mode, this article describes the goals, roles, application scenarios, advantages, and other content. examples of the adapter mode code in the PHP design mode

This article mainly introduces the Adapter mode code example of PHP design mode. This article describes the objectives, roles, application scenarios, advantages, and other content, and provides code examples. For more information, see

Objectives:

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

Role:

Target is adapted to the Target, which defines the interface for converting other classes, that is, our expected interface.

The Adaptee adapter is 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, data operations include mysql, mysqli, pdo, sqlite, postgresql, etc. if you need to change the database in the environment generated, you can use the Adapter mode to unify the interface. Similarly, the cache scenario is that it is more convenient to change the cache policy (memcache, redis, apc.

Advantages:

The adapter adapts to the target through the adapter to achieve the goal of being transparent to the customer.

Sample code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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 adaptation 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)

{

//...

}

}

// Use the client

$ Client = new Postgresql ();

$ Client-> query ($ SQL );

As shown above:

Target adaptation Target: IDataBase interface

Adaptee adapter: database operation functions of mysql and postgresql

Adapter: mysql and postgresql

Pipeline this article mainly introduces the Adapter mode code example of PHP design mode. This article describes the objectives, roles, application scenarios, advantages, and other content...

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.