[php]php设计模式 Adapter(适配器模式)

1 <?php
2 /**
3 * 适配器模式
4 *
5 * 将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作
6 */
7
8 // 这个是原有的类型
9 class OldCache
10 {
11 publicfunction __construct()
12 {
13 echo"OldCache construct<br/>";
14 }
15
16 publicfunction store($key,$value)
17 {
18 echo"OldCache store<br/>";
19 }
20
21 publicfunction remove($key)
22 {
23 echo"OldCache remove<br/>";
24 }
25
26 publicfunction fetch($key)
27 {
28 echo"OldCache fetch<br/>";
29 }
30 }
31
32 interface Cacheable
33 {
34 publicfunction set($key,$value);
35 publicfunction get($key);
36 publicfunction del($key);
37 }
38
39 class OldCacheAdapter implements Cacheable
40 {
41 private$_cache=null;
42 publicfunction __construct()
43 {
44 $this->_cache =new OldCache();
45 }
46
47 publicfunction set($key,$value)
48 {
49 return$this->_cache->store($key,$value);
50 }
51
52 publicfunction get($key)
53 {
54 return$this->_cache->fetch($key);
55 }
56
57 publicfunction del($key)
58 {
59 return$this->_cache->remove($key);
60 }
61 }
62
63 $objCache=new OldCacheAdapter();
64 $objCache->set("test",1);
65 $objCache->get("test");
66 $objCache->del("test",1);

posted on 2011-01-04 23:12  bluefrog  阅读(2401)  评论(1编辑  收藏  举报