<?php
/**
* 适配器模式
*
*/
/**
* 服务器端返回serialize后天气的数据
*
*/
class tianqi{
public static function show(){
$today = array('tep'=>28, 'wind'=>7, 'sun'=>'sunny');
return serialize($today);
}
}
/**
* 添加一个适配器,把serialize的字符串转成json,java客户端就可以调用了
*
*/
class AdapterTianqi extends tianqi{
public static function show(){
$today = parent::show();
$today = unserialize($today);
$today = json_encode($today);
return $today;
}
}
// php的客服端调用上面的api
$tq = unserialize(tianqi::show());
echo '温度:'.$tq['tep'].'<br />';
echo '风力:'.$tq['wind'].'<br />';
echo 'sun:'.$tq['sun'].'<br />';
echo '-------------------------------------------------------<br />';
// 来了一批手机的java客户端,不认识php的串行化字符串,怎么办?
// 把服务器代码改了?-- 旧的客户端又会受到影响?
$tq = json_decode(AdapterTianqi::show());
echo '温度:'.$tq->tep.'<br />';
echo '风力:'.$tq->wind.'<br />';
echo 'sun:'.$tq->sun.'<br />';