PHP设计模式及在框架设计中的经典应用.ppt

上传人:sh****n 文档编号:6347684 上传时间:2020-02-23 格式:PPT 页数:32 大小:4.32MB
返回 下载 相关 举报
PHP设计模式及在框架设计中的经典应用.ppt_第1页
第1页 / 共32页
PHP设计模式及在框架设计中的经典应用.ppt_第2页
第2页 / 共32页
PHP设计模式及在框架设计中的经典应用.ppt_第3页
第3页 / 共32页
点击查看更多>>
资源描述
PHP设计模式及在框架设计中的经典应用 韩友洪python 目标 了解设计模式与框架给出一个可行的学习设计模式的方法介绍几种常见的设计模式 Agenda 设计模式简述框架简述设计模式与框架软件开发演进过程框架要解决的问题FactorySingletonRegisterAdapterProxyActiveRecordMVC 什么是模式 demofor inti 0 i 100 i 主谓宾我爱你计算机领域 在特定场景下 解决某一类问题的通用方法模式分类 参见 POSA 1 架构模式设计模式惯用法 为什么要关注设计模式 向专家学习向历史学习大型软件设计 实施必备沟通交流的语言薪水 模式四要素 模式名称 patternname 交流 标记问题 problem 场景 上下文解决方案 solution 解决方法 方案效果 consequences 模式应用的效果及使用模式应权衡的问题 框架是什么 可复用的面向 对象 软件系统应用程序工具箱api 函数框架 Framework 框架解决特定领域里面具有类似问题的一组相互协作的类提供解决常见问题的通用组件为了 复用 已有的解决方案为了生产效率和可维护性 软件开发演进过程 机器语言 汇编语言高级语言 面向过程编程OOP 设计模式 元编程框架 DSL智能组件 只描述需求 软件就给您实现了 程序员担当 智能 设计自己的框架可能面临的问题 创建大量的复杂对象比较耗资源的对象只希望初始化一次动态处理大量的配置适应不同的数据库希望延迟初始化数据库连接日志处理性能监控 Warning 接下来的代码供示例使用 不具备产品级可用性 场景一 需要在很多函数中调用数据库的操作假设对数据库的操作已经封装在一个类里面实现v0可以在函数里面调用该类functionfoo db newDriver DB Mysql functionbar db newDriver DB Mysql 问题如果Driver DB Mysql 改名了呢 参数变化了呢 模式一 Factory 工厂模式V0 1ImplclassDB Factory publicstaticfunctiongetInstance db newDriver DB Mysql return db 使用functionfoo db DB Factory getInstance 使用一致的 简单的方式来初始化复杂的对象 场景二 在一个业务流程中要访问数据库资源 发现多次连接数据库 消耗资源 希望只连接接数据库一次实现v0全局变量中初始化连接 db Db Factory getInstance 业务访问全局变量functionfoo global db 问题badsmells 模式二 singleton 单例模式 工厂模式ImplclassDB Factory privatestatic db privatefunction clone privatefunction construct publicstaticfunctiongetInstance if self dbinstanceofDriver DB Mysql self db newDriver DB Mysql returnself db 使用functionfoo db DB Factory getInstance 实例化一个对象 共享连接 场景三 框架中需要保存一些全局的设置或类的实例 保存框架上下文实现v0使用系统 GLOBALS GLOBALS debug xxx 使用functionfoo if GLOBALS debug 问题badsmells违反职责SRP 模式三 Registry Registry FactoryImplclassRegistry privatestatic instance privatestatic data array private data array 感谢一位同学指出 用实例变量还是静态变量 依赖于整体的设计 需要对应修改set get方法 publicstaticfunctiongetInstance returnself instance publicfunctionset key value this data key value publicfunctionget key returnisset this data key this data key null 使用 ctx Reistry getInstance ctx set xxxx oooo value ctx get xxxx 使用单实例的全局对象来代替全局的变量 模式三 Registry Factory 续 保存读写分离的两个mysql连接保存 ctx Registry getInstance ctx set db read Db Factory getInstance db read ctx set db write Db Factory getInstance db read 使用 dbReadObj ctx get db read dbWriteObj ctx get db write 场景四 需要同时支持mysql和postgresql连接实现针对每个数据库 单独写一个驱动实例Db Factory getMysqlDB Db Factory getPostgresqlDB 问题如何保持不同DB接口的一致性 模式四 AdapterPattern 定义接口interfaceIDB publicfunctionconnect publicfunctionerror publicfunctionerrno publicstaticfunctionescape string string publicfunctionquery query publicfunctionfetchArray result publicfunctionfetchRow result publicfunctionfetchAssoc result publicfunctionfetchObject result publicfunctionnumRows result publicfunctionclose 接口定义供示例用 模式四 AdapterPattern 续 针对不同的DB实现Driver DB MysqlimplementsIDB private link publicfunctionconnect server username password new link true client flags 0 this link mysql connect server username password new link client flags publicfunctionerrno returnmysql errno this link 模式四 AdapterPattern 续 Adpater Factory实现DB Factory publicstaticfunctiongetInstance adapter Mysql 参数化工厂if isset self db adapter db adapter instanceofIDB if include once Drivers DB type php classname Driver DB ucfirst type self db adapter new classname else thrownewException Drivernotfound returnself db adapter 使用DB Factory getInstance mysql 场景五 需要延迟初始化一些资源classUserController private db 构造函数中初始化资源function construct this db DB Factory getInstance mysql publicfunctionmanages sql select frommanagers users tthis db getResults sql publicfunctionfoo echo haha 问题foo 方法不需要DB连接 在构造函数里面去连接数据库是不是额外消耗连接资源 模式五 Proxy 代理模式v1实现共同的接口classProxy DB MysqlimplementsIDB private db publicfunctionquery query if this db this db DB Factory getInstance mysql return this db query query 延迟初始化 把耗费资源的操作延迟到必须的时候问题每个方法都需重写一次 Proxy 代理模式v2使用magic方法classProxy DB Mysql private mysql publicfunction call method args if this mysql this mysql DB Factory getInstance mysql retruncall user func array array this mysql method args 使用 functionfoo this proxy query sql v1和v2这两种方式哪一个更好呢 场景六 每次我们都需要原生的sql functionfoo sql select fromusers 我希望OO 不要有那么多SQL我不懂SQLhowto nosql 模式六 ActiveRecord 使用示例 User newUser User name hanyh User address 北京 User save or data array name hanyh address beijing User save data ActiveRecord 实现classUserextendActiveBase CONSTINSERT SQL insertintouser name address values private data protected fields array name address publicfunctionsave data array if empty data this db save SELF INSERT SQL this data else this db save SELF INSERT SQL data privatefunction set field value if in array field this fields this data field value else thrownewException fielddoesnotexist 基类负责数据库连接 参数合法性校验等等 MVC模式 架构模式中的一种属于交互系统场景用户界面会变换界面的变换不影响核心功能性代码有一致的方式来区分并组织好存储 业务 显示相关代码 MVC MVC框架诞生的11个步骤 区分核心功能和用户交互Model实现变化通知功能 需要吗 参考Observer模式设计和实现view设计和实现Controller响应用户的输入 事件把View和Controller关联起来 怎么相互调用 MVC框架的初始化动态创建View 动态可扩展的插件系统controller不绑定到特定的view 复用一些基础库 建立层次性的继承体系 与当前业务解耦合 变成一个通用的框架 推广好酒也怕巷子深 抽象 学习方法 读好书站在巨人肩膀上Pattern OrientedSoftwareArchitecture 5本 聆听大师的教诲多实践纸上得来终觉浅 绝知此事要躬行做一个microframework多交流碰撞 谢谢大家 Q A
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!