-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adapter.php
110 lines (93 loc) · 1.87 KB
/
Adapter.php
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
<?php
/**
* 适配器模式
*
* 将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作
*/
// 这个是原有的类型
class OldCache
{
public function __construct()
{
echo "OldCache construct<br/>";
}
public function store($key,$value)
{
echo "OldCache store<br/>";
}
public function remove($key)
{
echo "OldCache remove<br/>";
}
public function fetch($key)
{
echo "OldCache fetch<br/>";
}
}
interface Cacheable
{
public function set($key,$value);
public function get($key);
public function del($key);
}
class OldCacheAdapter implements Cacheable
{
private $_cache = null;
public function __construct()
{
$this->_cache = new OldCache();
}
public function set($key,$value)
{
return $this->_cache->store($key,$value);
}
public function get($key)
{
return $this->_cache->fetch($key);
}
public function del($key)
{
return $this->_cache->remove($key);
}
}
$objCache = new OldCacheAdapter();
$objCache->set("test",1);
$objCache->get("test");
$objCache->del("test",1);
/*
// 也可以是接口
class Target
{
function Request()
{
echo "普通请求";
}
}
// 需要适配的类
class Adaptee
{
function SpecificRequest()
{
echo "特殊请求";
}
}
class Adapter extends Target
{
var $_adaptee = null;
function Adapater()
{
$this->_adaptee = new Adaptee();
}
function Request()
{
$this->_adaptee->Request();
}
}
$obj = new Adapter();
target.Request();
*/
/**
* 常用到的地方:
* 不同的数据库处理 不同的缓存处理
*/
?>