工厂模式学习报告ppt课件_第1页
工厂模式学习报告ppt课件_第2页
工厂模式学习报告ppt课件_第3页
工厂模式学习报告ppt课件_第4页
工厂模式学习报告ppt课件_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

设计模式 学习报告 移动互联子公司胡凯2003 08 07 概要 模式概论简单工厂模式工厂模式设计模式的实际应用总结相关资源 模式概论 什么是模式为什么要用模式设计模式的分类 什么是模式 模式 其实就是解决某一类问题的方法论 把解决某类问题的方法总结归纳到理论高度 那就是模式 Alexander给出的经典定义是 每个模式都描述了一个在我们的环境中不断出现的问题 然后描述了该问题的解决方案的核心 通过这种方式 你可以无数次地使用那些已有的解决方案 无需在重复相同的工作 模式有不同的领域 建筑领域有建筑模式 软件设计领域也有设计模式 因为每一个模式描述了一个在我们周围不断重复发生的问题 以及问题的解决方案的核心 它是一种指导 在一个良好的指导下 有助于你完成任务 有助于你作出一个优良的设计方案 达到事半功倍的效果 而且会得到解决问题的最佳办法 为什么要用模式 设计模式的分类 GOF的一书中提出了23种模式 而这23种模式又可划分为创建型模式 结构型模式 行为型模式 工厂模式则属于创建型模式 工厂模式专门负责讲大量有共同接口的类实例化 工厂模式可以动态决定将哪一个类实例化 不必事先知道每次要实例化哪一个类 工厂模式有以下几种形态 简单工厂 SimpleFactory 模式工厂方法 FactoryMethod 模式抽象工厂 AbstractFactory 模式 以下就这几个模式的结构和实例与大家分享一下 其他的我还不太懂 相信当中有不正确地方 还望大家提出 指正 简单工厂 SimpleFactory 模式 简单工厂模式的引进简单工厂模式的结构简单工厂模式的实现简单工厂模式的优缺点 简单工厂模式的引论 简单工厂模式 又称静态工厂方法模式 StaticFactoryMethodPattern 我们可以先来看一个实际生活中的例子 一个农场公司要向市场销售各类水果 有以下几类水果 葡萄Grape 草莓Strawberry 苹果Apple 考虑到它们的共性 我们一般是定义一个接口为水果 Fruit publicclassAppleimplementsFruit publicvoidgrow System out println Appleisgrowing publicvoidharvest System out println Applehasbeenharvested publicvoidplant System out println Applehasbeenplanted publicinterfaceFruit voidgrow voidharvest voidplant ClassApple 类的实现 publicclassGrapeimplementsFruit publicvoidgrow System out println Grapeisgrowing publicvoidharvest System out println Grapehasbeenharvested publicvoidplant System out println Grapehasbeenplanted publicinterfaceFruit voidgrow voidharvest voidplant ClassGrape publicclassStrawberryimplementsFruit publicvoidgrow System out println Strawberryisgrowing publicvoidharvest System out println Strawberryhasbeenharvested publicvoidplant System out println Strawberryhasbeenplanted publicinterfaceFruit voidgrow voidharvest voidplant ClassStrawberry publicclassFruitGardener publicstaticFruitfactory Stringwhich throwsBadFruitException if which equalsIgnoreCase apple returnnewApple elseif which equalsIgnoreCase strawberry returnnewStrawberry elseif which equalsIgnoreCase grape returnnewGrape else thrownewBadFruitException Badfruitrequest ClassFruitGardener 简单工厂模式的结构 FruitGardenerfactory Stringwhich 工厂类 Creator 角色 担任这个角色的是工厂方法模式的核心 含有与应用紧密相关的商业逻辑 具体由一个JAVA类实现 抽象产品 Product 角色 一般是所有公共产品的公共拥有的接口 可以是接口也可以是抽象类 具体产品 ConcreteProduct 角色 工厂模式所创建的任何对象都是这个角色 具体由一个JAVA类实现 Applet等 fruit 简单工厂模式的实现 多层次的产品结构使用JAVA接口或JAVA抽象类多个工厂方法 可以有多个工厂方法 抽象产品角色的省略工厂角色与抽象产品角色合并三个角色的合并 简单工厂模式的优点和缺点 优点 客户端可以免除直接创建产品对象的责任 而仅仅负责 消费 产品 实现了对责任的分割 缺点 这个工厂类里集中了所有的产品创建逻辑 当要增加产品时都需更改工厂类静态方法无法由子类继承 工厂角色无法形成等级结构 一个简单工厂模式的实例 工厂方法 FactoryMethod 模式 工厂方法模式的引进工厂方法模式的结构和角色工厂方法模式的实现简单模式和工厂方法模式 工厂方法模式的引进 回顾前面讨论的简单工厂模式的缺点 当有新的产品加入到系统中之后 就需要更改工厂类 将必要的逻辑加入到工厂类中 而这就有背与 开 闭的原则 扩展工厂类等级结构 简单工厂模式 工厂方法模式 开 闭 原则要求一个系统在无需修改的情况下扩展其功能 工厂方法模式的结构和角色 抽象工厂 Creator 角色具体工厂 ConcreteCreator 角色抽象产品 Product 角色具体产品 ConcreteProduct 角色 publicclassClient privatestaticCreatorcreator1 creator2 privatestaticProductprod1 prod2 publicstaticvoidmain String args creator1 newConcreteCreator1 prod1 creator1 factory creator2 newConcreteCreator2 prod2 creator2 factory publicinterfaceCreator publicProductfactory publicclassConcreteCreator1implementsCreator publicProductfactory returnnewConcreteProduct1 publicclassConcreteCreator2implementsCreator publicProductfactory returnnewConcreteProduct2 工厂类 publicinterfaceProduct publicclassConcreteProduct1implementsProduct publicConcreteProduct1 System out println CocnreteProduct1isbeingcreated publicclassConcreteProduct2implementsProduct publicConcreteProduct2 System out println CocnreteProduct2isbeingcreated 产品类 publicclassClient privatestaticCreatorcreator1 creator2 privatestaticProductprod1 prod2 publicstaticvoidmain String args creator1 newConcreteCreator1 prod1 creator1 factory creator2 newConcreteCreator2 prod2 creator2 factory 活动序列图 客户端创建一个ConcreteCreator1对象 然后调用ConcreteProduct1的构造函数创建出产品对象 creator1 newConcreteCreator1 prod1 creator1 factory 简单工厂模式和工厂方法模式 不同之处 工厂模式的核心是一个抽象工厂类 而简单工厂模式把核心放在一个具体类上 表现在当要加入一个新的产品的时候 并不需要更改工厂类 而只需要扩展抽象工厂类 实现了工厂等级结构 符合 开 闭 原则的要求 相同之处 Factory 方法返回的数据类型是一个抽象类型 而不是一种具体产品类型 而客户端也不必知道所得到的真实类型 设计模式的实际应用 ConnectionFactory abstract MEIPDataSourceConnection class MEIPDriverConnection class staticnewInstance 所在包 com creawor meip db publicinterfaceCreator publicabstractConnectiongetConnection ConnectionFactorystaticnewInstance 单实例模式singleton 抽象工厂和抽象产品和为一个抽象类 继承getConnection 方法得到具体类型的Connection 具体产品 FormatFactory abstract ExcelFormat class ContentFormat class staticnewInstance 所在包 com creawor meip format publicinterfaceCreator publicabstractintparse

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论