<?php /** * Created by PhpStorm. * User: 老鲁 * Date: 2018/3/16 * Time: 21:48 */ class Regitsration { public function regist(Lesson $lesson) { //处理课程 //通知 $notifierObj = Notifier::getNotifier(); $notifierObj->inform(); } } abstract class Notifier { static function getNotifier() { //根据需要和条件设置不同的返回对象 $num = rand(1,2); if ($num == 1) { return new TextNotifier(); } elseif ($num == 2){ return new MailNotifier(); } } } class TextNotifier extends Notifier { public function inform() { return 'TextInform'; } } class MailNotifier extends Notifier { public function inform() { return 'MailInform'; } }
PHP面向对象设计模式-8.2降低耦合示例
最新推荐文章于 2025-12-04 09:53:33 发布
本文介绍了一个简单的课程注册系统及通知机制实现。通过抽象类Notifier实现了通知方式的解耦,根据随机选择返回短信通知(TextNotifier)或邮件通知(MailNotifier)实例。此机制提高了系统的灵活性和扩展性。

1115

被折叠的 条评论
为什么被折叠?



