19463331韦德国际php设计方式 Singleton_php本领_脚本之

2019-11-23 23:08 来源:未知

_command[] = $command; } public function executeCommand() { foreach($this->_command as $command) { $command->execute(); } } public function removeCommand { $key = array_search($command, $this->_command); if { unset($this->_19463331韦德国际,command[$key]); } } } class Receiver { private $_name = null; public function __construct { $this->_name = $name; } public function action() { echo $this->_name." action
"; } public function action1() { echo $this->_name." action1
"; } } class ConcreteCommand implements Command { private $_receiver; public function __construct { $this->_receiver = $receiver; } public function execute() { $this->_receiver->action(); } } class ConcreteCommand1 implements Command { private $_receiver; public function __construct { $this->_receiver = $receiver; } public function execute() { $this->_receiver->action1(); } } class ConcreteCommand2 implements Command { private $_receiver; public function __construct { $this->_receiver = $receiver; } public function execute() { $this->_receiver->action(); $this->_receiver->action1(); } } $objRecevier = new Receiver; $objRecevier1 = new Receiver; $objRecevier2 = new Receiver; $objCommand = new ConcreteCommand; $objCommand1 = new ConcreteCommand1; $objCommand2 = new ConcreteCommand; $objCommand3 = new ConcreteCommand1; $objCommand4 = new ConcreteCommand2; // 使用 Recevier的两个方法 $objInvoker = new Invoker(); $objInvoker->setCommand; $objInvoker->setCommand; $objInvoker->executeCommand(); $objInvoker->removeCommand; $objInvoker->executeCommand(); $objInvoker->setCommand; $objInvoker->setCommand; $objInvoker->setCommand; $objInvoker->executeCommand(); [/code]

抽象策略角色:定义所有支持的算法的公共接口。通常是以一个接口或抽象来实现。Context使用这个接口来调用其ConcreteStrategy定义的算法。

复制代码 代码如下:

复制代码 代码如下:

具体策略角色:以Strategy接口实现某具体算法。

_db = new MysqlDB(); // 这里的不能进行操作 } /** * 获取处理 * * @param array $filter // 过滤条件 * @param string $field // 获取字段 * @param int $page // 当前页 * @param int $limit // 页数 */ function fetch,$field = "*",$page = 1,$limit = null) { $this->_db->select->from->where->limit; return $this->_db->execute(); } function update{} function insert(){} } class MemberDAO extends BaseDAO { var $_table = "member"; } $oMember = new MemberDAO; /** * 常用到的地方: * MVC中model层基类 */ ?>

display(); $obj1 = Singleton::getInstance(); var_dump;

环境角色:持有一个Strategy类的引用,用一个ConcreteStrategy对象来配置

_strategy = $strategy; } public function contextInterface() { $this->_strategy->algorithmInterface(); }}// client$strategyA = new ConcreteStrategyA();$context = new Context;$context->contextInterface();$strategyB = new ConcreteStrategyB();$context = new Context;$context->contextInterface();$strategyC = new ConcreteStrategyC();$context = new Context;$context->contextInterface();

"; } public function get { return false; } public function set { return true; } public function del { return false; } } // 文件缓存 class FileCache implements CacheTable { public function __construct() { echo "Use FileCache
"; // 文件缓存构造函数 } public function get { // 文件缓存的get方法实现 } public function set { // 文件缓存的set方法实现 } public function del { // 文件缓存的del方法实现 } } // TTServer class TTCache implements CacheTable { public function __construct() { echo "Use TTCache
"; // TTServer缓存构造函数 } public function get { // TTServer缓存的get方法实现 } public function set { // TTServer缓存的set方法实现 } public function del { // TTServer缓存的del方法实现 } } // -- 以下是使用不用缓存的策略 ------ class Model { private $_cache; public function __construct() { $this->_cache = new NoCache(); } public function setCache { $this->_cache = $cache; } } class UserModel extends Model { } class PorductModel extends Model { public function __construct() { $this->_cache = new TTCache(); } } // -- 实例一下 --- $mdlUser = new UserModel(); $mdlProduct = new PorductModel(); $mdlProduct->setCache; // 改变缓存策略 ?>

具体的大家可以多关注一下脚本之家以前发布的文章

版权声明:本文由19463331韦德国际发布于韦德国际1946手机版,转载请注明出处:19463331韦德国际php设计方式 Singleton_php本领_脚本之