PHP设计模式 五 (观察者 策略 装饰器模式)
来源:互联网 发布:淘宝卖家用什么app聊天 编辑:程序博客网 时间:2024/06/05 19:23
观察者模式
观察者模式(有时又被称为发布-订阅Subscribe>模式、模型-视图View>模式、源-收听者Listener>模式或从属者模式)是软件设计模式的一种。在此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。这通常透过呼叫各观察者所提供的方法来实现。此种模式通常被用来实现事件处理系统。
当一个对象的状态发生改变时,依赖他的对象全部会接到通知,并自动更新。观察者模式实现了低耦合 非入侵式的通知与更新机制。
观察者模式示例:
首先创建一个事件生成器 EventGenerator.php
<?phpnamespace Components\ObServer;abstract class EventGenerator {private $obServers = array();function addObServer(IObServer $obServer) {//添加事件$this->obServers[] = $obServer;}function notify() {foreach ($this->obServers as $obServer) {//一一执行更新$obServer->update();}}}
创建一个观察者接口IObServer.php
<?phpnamespace Components\ObServer;interface IObServer {function update($event_info = NULL);}
事件主体:
class Event extends \Components\ObServer\EventGenerator{function tringger() {echo "<br>Event<br/>";$this->notify();}}
观察者:
//观察者1class ObServer1 implements \Components\ObServer\IObServer {function update($event_info = null) {echo "logic1<br>";}}//观察者2class ObServer2 implements \Components\ObServer\IObServer {function update($event_info = null) {echo "logic2<br>";}}
触发:
$event = new Event();$event->addObServer(new ObServer1());$event->addObServer(new ObServer2());$event->tringger();
策略模式
将一组特定的行为和算法封装成类,以适应某些特定的上下文环境,这就是策略模式。
应用场景:
1、 多个类只区别在表现行为不同,可以使用Strategy模式,在运行时动态选择具体要执行的行为。
2、 需要在不同情况下使用不同的策略(算法),或者策略还可能在未来用其它方式来实现。
3、 对客户隐藏具体策略(算法)的实现细节,彼此完全独立。
组成
1、抽象策略角色: 策略类,通常由一个接口或者抽象类实现。
2、具体策略角色:包装了相关的算法和行为。
3、环境角色:持有一个策略类的引用,最终给客户端调用。
分类广告策略示例:
抽象策略角色
vim Istrategy.php
<?phpnamespace Components\Strategy;/** * 策略模式 接口 * @author YUNDUAN * */interface IStrategy{function showAd();function showCategory();}
具体策略角色
vim FemaleUser.php
<?phpnamespace Components\Strategy;class FemaleUser implements IStrategy {function showAd() {echo '女性广告<br>';}function showCategory() {echo '女装<br>';}}
vim MaleUser.php
<?phpnamespace Components\Strategy;class MaleUser implements IStrategy {function showAd() {echo '男性广告<br>';}function showCategory() {echo '电子产品<br>';}}
环境角色:
vim Home.php
/** * 策略模式 解耦和 * @author YUNDUAN * */class Home {protected $strategy;function index() {echo "AD:";$this->strategy->showAd();echo "Category:";$this->strategy->showCategory();}function setStrategy(Components\Strategy\IStrategy $strategy) { //依赖注入 注入一个对象$this->strategy = $strategy;}}
使用:
$home = new Home();if (isset($_GET['female'])) {$strategy = new \Components\Strategy\FemaleUser();}else {$strategy = new \Components\Strategy\MaleUser();}/*策略模式*/$home->setStrategy($strategy);$home->index();
装饰器模式
在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
装饰器接口IDecorator.php:
<?phpnamespace Components\Decorator;/** * 装饰器模式 接口 * @author WH * */interface IDecorator {function before();function after();}
装饰器1 DecoratorAttr1.php:
<?phpnamespace Components\Decorator;class DecoratorAttr1 implements IDecorator{function before() {echo '<br><div style="color:red">';}function after() {echo '</div>';}}
装饰器2 DecoratorAttr2.php:
<?phpnamespace Components\Decorator;class DecoratorAttr2 implements IDecorator{function before() {echo '<br><div style="font-size:30px">';}function after() {echo '</div>';}}
使用:
/* 装饰器模式 原类 */class DecoratorClassTest {protected $decorators = array();//添加装饰器function addDecorator(\Components\Decorator\IDecorator $decorator) {$this->decorators[] = $decorator;//添加装饰器}function before(){foreach ($this->decorators as $decorator) {$decorator->before();//调用装饰器}}function after(){//这里可用栈$decorators = array_reverse($this->decorators);foreach ($decorators as $decorator) {$decorator->after();//结束装饰器}}function test() {$this->before();echo 'running<be>';$this->after();}}
$decoratorTest = new DecoratorClassTest();$decoratorTest->addDecorator(new \Components\Decorator\DecoratorAttr1());$decoratorTest->addDecorator(new \Components\Decorator\DecoratorAttr2());$decoratorTest->test();
策略模式
将一组特定的行为和算法封装成类,以适应某些特定的上下文环境,这就是策略模式。
应用场景:
1、 多个类只区别在表现行为不同,可以使用Strategy模式,在运行时动态选择具体要执行的行为。
2、 需要在不同情况下使用不同的策略(算法),或者策略还可能在未来用其它方式来实现。
3、 对客户隐藏具体策略(算法)的实现细节,彼此完全独立。
组成
1、抽象策略角色: 策略类,通常由一个接口或者抽象类实现。
2、具体策略角色:包装了相关的算法和行为。
3、环境角色:持有一个策略类的引用,最终给客户端调用。
分类广告策略示例:
抽象策略角色
vim Istrategy.php
<?phpnamespace Components\Strategy;/** * 策略模式 接口 * @author YUNDUAN * */interface IStrategy{function showAd();function showCategory();}
具体策略角色
vim FemaleUser.php
<?phpnamespace Components\Strategy;class FemaleUser implements IStrategy {function showAd() {echo '女性广告<br>';}function showCategory() {echo '女装<br>';}}
vim MaleUser.php
<?phpnamespace Components\Strategy;class MaleUser implements IStrategy {function showAd() {echo '男性广告<br>';}function showCategory() {echo '电子产品<br>';}}
环境角色:
vim Home.php
/** * 策略模式 解耦和 * @author YUNDUAN * */class Home {protected $strategy;function index() {echo "AD:";$this->strategy->showAd();echo "Category:";$this->strategy->showCategory();}function setStrategy(Components\Strategy\IStrategy $strategy) { //依赖注入 注入一个对象$this->strategy = $strategy;}}
使用:
$home = new Home();if (isset($_GET['female'])) {$strategy = new \Components\Strategy\FemaleUser();}else {$strategy = new \Components\Strategy\MaleUser();}/*策略模式*/$home->setStrategy($strategy);$home->index();
- PHP设计模式 五 (观察者 策略 装饰器模式)
- 策略模式+观察者模式+装饰者模式
- java 策略设计模式 装饰器模式
- 慕课网----大话PHP设计模式 五(原型模式,装饰器模式,迭代器模式,代理模式)
- 设计模式-概念(策略模式,观察者模式,装饰者模式)
- php设计模式--单例,工厂,观察者,策略模式
- 【设计模式】【五】观察者模式
- 【设计模式 五】观察者模式
- 设计模式(五)--装饰模式
- 设计模式---策略模式And 观察者模式
- 设计模式---策略模式和观察者模式
- 设计模式[策略模式和观察者模式]
- 设计模式--策略模式,装饰模式
- PHP设计模式-装饰器模式
- PHP设计模式-装饰器模式
- php设计模式之装饰器模式
- 【设计模式】之装饰器模式-PHP
- PHP设计模式之装饰器模式
- 黑马程序员——C语言基础知识整理——数据类型、常量与变量
- Andorid--AsyncHttpClient(一)
- 图片生成database 64
- 开启Windows 8自带Hyper-V功能创建虚拟机
- Sublime的SVN插件
- PHP设计模式 五 (观察者 策略 装饰器模式)
- 杭电1212 Big Number (大数求模运算)
- Android的5大布局
- LeetCode Remove Nth Node From End of List
- 直方图均衡化matlab实现(续)
- Window卸载Java出错:无法访问windows installer服务
- Andorid--AsyncHttpClient(二)
- OpenCV3.0编译安装
- android开发之Android应用性能评测调优