banner
NEWS LETTER

适配器模式

Scroll down

什么是适配器模式

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。
这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。
举个栗子:
PHP常用的操作数据库的方法有三种:MySQL、MySQLi和PDO,需要怎么去适配呢,又或者说,还有Oracle、非关系型数据库(Memcache、Redis)又怎么去适配呢。

适配器模式的实现

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

/**
* 数据库适配器
* Interface DatabaseAdapter
*/
interface DatabaseAdapter
{
/**
* 连接数据库
* @param string $host
* @param int $port
* @param string $username
* @param string $password
* @param string $dbname
* @return mixed
*/
public function connect(string $host, int $port, string $username, string $password, string $dbname);

/**
* 执行sql
* @param string $sql
* @return mixed
*/
public function query(string $sql);

/**
* 关闭连接
* @return mixed
*/
public function close();
}

/**
* Class MySQLi
*/
class MySQLi implements DatabaseAdapter
{
/**
* 连接数据库
* @param string $host
* @param int $port
* @param string $username
* @param string $password
* @param string $dbname
* @return mixed
*/
public function connect(string $host, int $port, string $username, string $password, string $dbname)
{
// TODO: Implement connect() method.
}

/**
* 执行sql
* @param string $sql
* @return mixed
*/
public function query(string $sql)
{
// TODO: Implement query() method.
}

/**
* 关闭连接
* @return mixed
*/
public function close()
{
// TODO: Implement close() method.
}
}

/**
* Class MySQL
*/
class MySQL implements DatabaseAdapter
{

/**
* 连接数据库
* @param string $host
* @param int $port
* @param string $username
* @param string $password
* @param string $dbname
* @return mixed
*/
public function connect(string $host, int $port, string $username, string $password, string $dbname)
{
// TODO: Implement connect() method.
}

/**
* 执行sql
* @param string $sql
* @return mixed
*/
public function query(string $sql)
{
// TODO: Implement query() method.
}

/**
* 关闭连接
* @return mixed
*/
public function close()
{
// TODO: Implement close() method.
}
}

/**
* Class PDO
*/
class PDO implements DatabaseAdapter
{

/**
* 连接数据库
* @param string $host
* @param int $port
* @param string $username
* @param string $password
* @param string $dbname
* @return mixed
*/
public function connect(string $host, int $port, string $username, string $password, string $dbname)
{
// TODO: Implement connect() method.
}

/**
* 执行sql
* @param string $sql
* @return mixed
*/
public function query(string $sql)
{
// TODO: Implement query() method.
}

/**
* 关闭连接
* @return mixed
*/
public function close()
{
// TODO: Implement close() method.
}
}
其他文章
cover
责任链模式
  • 23/10/02
  • 23:19
  • 615
  • 2
cover
Hello World
  • 23/10/02
  • 19:25
  • 8
  • 1
目录导航
  1. 1. 什么是适配器模式
  2. 2. 适配器模式的实现
请输入关键词进行搜索