<?php /** * @author v.r And * * @example * 外觀模式 * 經過在必要的邏輯和方法的集合建立簡單外觀接口,外觀設計模式隱藏了來自調用對象的複雜性 * 實際點 * 基於外觀設計模式的類可能提供執行一個邏輯業務請求的公共接口。爲了完成這個單獨的業務請求 * 可能須要執行多個技術邏輯步驟 * * 數據輸出 json與XML格式 * @copyright copyright information * */ class CD { public $tracks = array(); //曲目 public $band = ''; //樂隊 public $title = ''; //標題 public function __construct($title='',$band='',$tracks=array()) { # code... $this->title = $title; $this->band = $band; $this->tracks = $tracks; } } $tracks = array('the word' ,'Ghost trow','goods very' ); $title = 'waste Rib o '; $band = 'Never Again'; $cd = new CD(); class CDUpperCase { static public function makestring(CD $cd,$types) { $key = array_keys($types); $value = array_values($types); $type = $key[0]; $value =$value[0]; $cd->$type = strtoupper($value); } static public function makeArray(CD $cd,$types) { $key = array_keys($types); $value = array_values($types); $type = $key[0]; $value = $value[0]; $cd->$type = array_map('strtoupper', $value); } } class CDMakeXML { static public function create(CD $cd) { $doc = new DomDocument(); $root = $doc->createElement('CD'); $root = $doc->appendChild($root); $title = $doc->createElement('TITLE',$cd->title); $title = $root->appendChild($title); $band = $doc->createElement('BAND',$cd->band); $band = $root->appendChild($band); $tracks = $doc->createElement('TRACKS'); $tracks = $root->appendChild($tracks); foreach ($cd->tracks as $track) { $track = $doc->createElement('TRACK',$track); $track = $tracks->appendChild($track); } return $doc->saveXML(); } } class CDMakeJSON { static public function create(CD $cd) { return json_encode(get_object_vars($cd)); } } CDUpperCase :: makestring($cd,array('title'=>'waste Rib o')); CDUpperCase :: makestring($cd,array('band'=>'Never Again')); CDUpperCase :: makeArray($cd,array('tracks'=>array('the word' ,'Ghost trow','goods very'))); $xml = CDMakeXML :: create($cd); # 外觀 class WebServiceFacade { static public function makeXML(CD $cd) { # code... CDUpperCase :: makestring($cd,array('title'=>'waste Rib o')); CDUpperCase :: makestring($cd,array('band'=>'Never Again')); CDUpperCase :: makeArray($cd,array('tracks'=>array('the word' ,'Ghost trow','goods very'))); return CDMakeXML :: create($cd); } static public function makeJSON(CD $cd) { CDUpperCase :: makestring($cd,array('title'=>'waste Rib o')); CDUpperCase :: makestring($cd,array('band'=>'Never Again')); CDUpperCase :: makeArray($cd,array('tracks'=>array('the word' ,'Ghost trow','goods very'))); return CDMakeJSON :: create($cd); # code... } } $xml = WebServiceFacade :: makeXML(new CD); $json = WebServiceFacade :: makeJSON(new CD); var_dump($xml); var_dump($json); #end script