享元模式 的另外一个例证
2015-11-27 14:06
176 查看
<?php // 抽象享元角色 abstract class Flyweight { abstract public function operation($state); } // 具体享元角色 class ConcreteFlyweight extends Flyweight { private $_intrinsicState = null; public function __construct($state) { echo $state; $this->_intrinsicState = $state; } public function operation($state) { echo $state; } } // 不共享的具体享元,客户端直接调用 class UnsharedConcreteFlyweight extends Flyweight { private $_intrinsicState = null; public function __construct($state) { echo $state; $this->_intrinsicState = $state; } public function operation($state) { echo $state; } } // 享元工厂角色 class FlyweightFactory { private $_flyweights; public function __construct() { $this->_flyweights = array(); } public function getFlyweigth($state) { if (isset($this->_flyweights[$state])) { return $this->_flyweights[$state]; } else { return $this->_flyweights[$state] = new ConcreteFlyweight($state); } } } // client $flyweightFactory = new FlyweightFactory(); //新建一个享元工厂角色 $flyweight = $flyweightFactory->getFlyweigth('state A'); $flyweight->operation('other state A'); $flyweight = $flyweightFactory->getFlyweigth('state B'); $flyweight->operation('other state B'); // 不共享的对象,单独调用 $uflyweight = new UnsharedConcreteFlyweight('state A'); $uflyweight->operation('other state A'); ?>
相关文章推荐
- 搭建SpringMVC+Mybatis框架并实现数据库的操作
- JavaSwing JScrollPane的使用
- Win10系统出现蓝屏提示错误代码0x00000050两种解决方法
- css position详解
- Go的异常处理 defer, panic, recover
- UNIX环境编程学习笔记------编程实例----域名解析函数的函数原型即getaddrinfo()函数原型
- 获取一组Text值
- Java计算幂的指数
- Nginx配置SSL证书部署HTTPS网站
- 魅族多机房部署方案
- C++ 抽象类
- [Leetcode] Valid Parentheses
- Daily Scrum & Project Team Meeting Review - 11/27
- VS2008mfc文本文档初始界面最大化
- 1096. Consecutive Factors (20)
- 《Java多线程编程实战指南(设计模式篇)》答疑总结(陆续更新)
- eclipse 编译servlet项目,找不到javax.servlet
- Volley进行GET请求,需要Header的解决办法
- 足球赛事法甲联赛接口
- 编程基础知识之编译程序