您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
设计模式-适配器模式
发布时间:2017-11-30 09:25:45编辑:雪饮阅读()
<?php
header('content-type:text/html;charset=utf-8');
//适配器模式
//服务端代码
class tianqi{
public static function show(){
$today=array('tep'=>28,'wind'=>7,'sun'=>'sunny');
return $today;
}
}
//增加一个适配器(适配xxx客户端)
class AdapterTianqi extends tianqi{
public static function show(){
$today=parent::show();
$today=json_decode(json_encode($today));
return $today;
}
}
//php客户端可以直接调用
$tq=tianqi::show();
echo '温度:'.$tq['tep'].'---风力:'.$tq['wind'].'---sun:'.$tq['sun'].'<br/>';
//xxx客户端只支持"对象->属性"这种方式调用而不能直接像"$tq['tep']"这样调用
$tq=AdapterTianqi::show();
echo '温度:'.$tq->tep.'---风力:'.$tq->wind.'---sun:'.$tq->sun.'<br/>';
?>
关键字词:设计模式,适配器模式