标签:
策略模式设计帮助构建的对象不必自身包含逻辑,而是能够根据需要利用其他对象中的算法。
<?php //策略模式 //cd类 class cd { protected $cdArr; public function __construct($title, $info) { $this->cdArr[‘title‘] = $title; $this->cdArr[‘info‘] = $info; } public function getCd($typeObj) { return $typeObj->get($this->cdArr); } } class json { public function get($return_data) { return json_encode($return_data); } } class xml { public function get($return_data) { $xml = ‘<?xml version="1.0" encoding="utf-8"?>‘; $xml .= ‘<return>‘; $xml .= ‘<data>‘ .serialize($return_data). ‘</data>‘; $xml .= ‘</return>‘; return $xml; } } $cd = new cd(‘cd_1‘, ‘cd_1‘); echo $cd->getCd(new json); echo $cd->getCd(new xml);
转自:http://blog.csdn.net/initphp/article/details/7760383
标签:
原文地址:http://www.cnblogs.com/zhhtao/p/4414719.html