php观察者模式应用场景实例详解_php技巧

PHP中文网
Release: 2016-05-16 16:45:52
Original
1652 people have browsed it

这篇文章主要介绍了php观察者模式应用场景,结合完整实例形式详细分析了php观察者模式的具体定义与使用技巧,需要的朋友可以参考下

本文实例讲述了php观察者模式的应用。分享给大家供大家参考,具体如下:

_observers as $obs ) $obs->onBuyTicketOver ( $this, $ticket ); //$this 可用来获取主题类句柄,在通知中使用 } //添加通知 public function addObserver($observer) //添加N个通知 { $this->_observers [] = $observer; } } #=========================定义多个通知==================== //短信日志通知 class HipiaoMSM implements ITicketObserver { public function onBuyTicketOver($sender, $ticket) { echo (date ( 'Y-m-d H:i:s' ) . " 短信日志记录:购票成功:$ticket
"); } } //文本日志通知 class HipiaoTxt implements ITicketObserver { public function onBuyTicketOver($sender, $ticket) { echo (date ( 'Y-m-d H:i:s' ) . " 文本日志记录:购票成功:$ticket
"); } } //抵扣卷赠送通知 class HipiaoDiKou implements ITicketObserver { public function onBuyTicketOver($sender, $ticket) { echo (date ( 'Y-m-d H:i:s' ) . " 赠送抵扣卷:购票成功:$ticket 赠送10元抵扣卷1张。
"); } } #============================用户购票==================== $buy = new HipiaoBuy (); $buy->addObserver ( new HipiaoMSM () ); //根据不同业务逻辑加入各种通知 $buy->addObserver ( new HipiaoTxt () ); $buy->addObserver ( new HipiaoDiKou () ); //购票 $buy->buyTicket ( "一排一号" ); ?>
Copy after login

运行结果如下:

2017-02-03 10:25:45 短信日志记录:购票成功:一排一号 2017-02-03 10:25:45 文本日志记录:购票成功:一排一号 2017-02-03 10:25:45 赠送抵扣卷:购票成功:一排一号 赠送10元抵扣卷1张。
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!