-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapterPattern.php
More file actions
56 lines (49 loc) · 1.12 KB
/
Copy pathAdapterPattern.php
File metadata and controls
56 lines (49 loc) · 1.12 KB
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
<?php
/**
* This is adapter pattern of design pattern
* 适配器模式是一种结构型模式,它将一个类的接口转接成用户所期待的.
* 一个适配器使得因接口不兼容而不能在一起工作的类工作在一起,做法是将类别自己的接口包裹在一个已存在的类中
*
* @author hkf <[email protected]>
* @version 0.1.0
*/
/**
* Interface Target
* 目标接口
*/
interface Target
{
public function sampleMethod1();
public function sampleMethod2();
}
/**
* Class Adaptee
* 源角色
*/
class Adaptee
{
public function sampleMethod1()
{
return 'this is method1 of adatee';
}
}
class Adapter extends Adaptee implements Target
{
private $_adaptee;
public function __construct(Adaptee $adaptee)
{
$this->_adaptee = $adaptee;
}
public function sampleMethod1()
{
return $this->_adaptee->sampleMethod1();
}
public function sampleMethod2()
{
return 'this is method2 of adapter';
}
}
$adaptee = new Adaptee();
$adapter = new Adapter($adaptee);
echo $adapter->sampleMethod1();
echo $adapter->sampleMethod2();