本文实例讲述了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
|
<?php
//适配器模式-通过适配器去执行第三方方法
//定义目标接口
interface Target{
public function simpleMethod1();
public function simpleMethod2();
}
class Adatee{
public function simpleMethod1(){
echo 'Adatee simpleMethod1<br/>' ;
}
}
//类适配器模式
class Adapter implements Target{
private $adatee ;
public function __construct(Adatee $adatee ){
$this ->adatee = $adatee ;
}
public function simpleMethod1(){
echo $this ->adatee->simpleMethod1();
}
public function simpleMethod2(){
echo $this ->adatee->simpleMethod12();
}
}
//客户端接口
class Client{
public static function main(){
$adapter = new Adapter( new Adatee());
$adapter ->simpleMethod1();
}
}
Client::main();
|
希望本文所述对大家PHP程序设计有所帮助。