chp8-设计模式实例

上传人:豆浆 文档编号:48597487 上传时间:2018-07-17 格式:PPT 页数:154 大小:3.10MB
返回 下载 相关 举报
chp8-设计模式实例_第1页
第1页 / 共154页
chp8-设计模式实例_第2页
第2页 / 共154页
chp8-设计模式实例_第3页
第3页 / 共154页
chp8-设计模式实例_第4页
第4页 / 共154页
chp8-设计模式实例_第5页
第5页 / 共154页
点击查看更多>>
资源描述

《chp8-设计模式实例》由会员分享,可在线阅读,更多相关《chp8-设计模式实例(154页珍藏版)》请在金锄头文库上搜索。

1、Design PatternsDesign Patterns (1 1)电子科技大学 计算机学院.软件学院1TP-154设计模式设计模式按类型划分按类型划分n创建型模式q以灵活的方式创建对象的集合n结构型模式q代表相关对象的集合,q如何正确地组合类或对象n行为型模式q在对象集合中捕获行为,q解决类或对象之间如何交互和如何分配职责的问题2TP-154Creational Design PatternCreational Design Patternn有助于我们设计包含对象集的应用程序,从单 一的代码中创建几个可能的集合q运行时可以创建集合的多个版本q约束创建的对象:比如确保类只有一个实例nFac

2、tory nAbstract Factory nPrototype nSingleton n3TP-154在运行时以灵活的方法创建对象,这些对象是构造方法不能单独提供的Factory的目标是处理简单的创建性情况;Use methods to return required objects.通过使用返回对象的方法来创建希望的对象客户类和工厂类分开。消费者任何时候需要某种产品,只需向工厂请求即可。Factory 设计模式4TP-154FactoryFactory Class Model Class ModelFactory design patternMyClasscreateObjectOfRe

3、quiredClass(): RequiredClass create objectRequiredClassClientRequiredClass类和MyClass类可以是同一类或同种类MyClass类可以拥有一个静态工厂方式创建自身的一个实例5TP-154Application of Factory design patternFactoryFactory Example ExampleBMW createAutomobile()BYD createAutomobile()AutomobilecreateAutomobile(): AutomobileClientcreate object

4、create object运行时,通过方法返回 一个新的所需对象!6TP-154Sequence Diagram for Sequence Diagram for FactoryFactorycreateObjectOfRequiredClass():MyClass:ClientRequiredClass():RequiredClass7TP-154Typical Output of E-Mail Generation ExampleTypical Output of E-Mail Generation Example从一个单一的控制代码版本中,产生一个适合各种用户的 邮件信息USE CASE

5、: 询问顾客类型;用户选择类型;程序向控制台回送顾客类型,打印Email信息8TP-154Design Goals Design Goals Correctness and Reusability Correctness and Reusability p通过分离代码段,程序适用于所有顾客类型;p可以更容易地检查设计的正确性和重用部分!9TP-154Application of Factory design patternCustomergetMessage()Client sendMessage()Frequent getMessage()Returning getMessage()Curi

6、ous getMessage()Newbie getMessage()MailMessage textMailGenerationApplication getCustomerTypeFromUser()setupFactoryFactory: Email Generation Example: Email Generation Example10TP-154小结小结n当单独使用构造方法不适用于对象的创建时, 就使用返回对象的方法nFactory是DP的委托形式,每个工厂方法都是 将创建责任委托给构造方法n工厂模式通过确保创建的类服从于某种约束, 从而增强了程序的健壮性。11TP-154提供一

7、个创建互相关联对象的族或者相互依赖对象的接口;不指明它们的具体类。族或者风格,Style?Abstract Factory Design Pattern Again设计一个应用程序,在这个应用程序中,有几种可能的对象集合风格,使用协同的工厂方法捕获这些风格!12TP-154Abstract FactoryAbstract Factory Design Pattern Design PatternStyle getComponentA() getComponentB()Client doOperation( Style myStyle )Style1 getComponentA() getCom

8、ponentB()Style2 getComponentA() getComponentB()ComponentA ComponentBStyle1ComponentAStyle1ComponentBStyle2ComponentAStyle2ComponentBCollection13TP-154Abstract FactoryAbstract Factory Design Pattern Design Pattern Applied to Applied to KitchenViewerKitchenViewerKitchenStyle getWallCabinet() getFloorC

9、abinet()Kitchen getWallCabinet() getFloorcabinet()Client renderKitchen( KitchenStyle )ModernKStyle getWallCabinet() getFloorCabinet()AntiqueKStyle getWallCabinet() getFloorCabinet()WallCabinet FloorCabinet ModernWallCabinetModernFloorCabinetAntiqueWallCabinetAntiqueFloorCabinet14TP-154CASE:Word Proc

10、essor Interaction CASE:Word Processor Interaction 1 /21 /2- Enter title:My Life- Enter Heading or “-done”:Birth- Enter text:I was born in a small mountain hut .- Enter Heading or “-done”:Youth- Enter text:I grew up playing in the woods - Enter Heading or “-done”: Adulthood. - Enter Heading or “-done

11、”: -done(continued)15TP-154Word Processor Interaction Word Processor Interaction 2 /2 2 /2 : : Output OptionsOutput Options. Enter the style you want displayed: big- TITLE: MY LIFE -SECTION 1. - BIRTH - I WAS BORN IN A MOUNTAIN HUT .SECTION 2. - YOUTH - I GREW UP STURDY SECTION 3. - ADULTHOOD - . En

12、ter the style you want displayed: smallmy lifebirth i was born in a mountain hut .youth i grew up sturdy adulthoodOption 2Option 116TP-154Abstract Factory*Abstract Factory Abstract Factory InterfaceInterfaceStyle.ClientStyleAFactoryStyleBFactoryEnsemble setAbstractFactory() doAFunction()AbstractFact

13、ory getAPart1Object() getAPart2Object()* relationships within pattern application not shown集合体:客户代码可以访问的使用风 格构造的实体17TP-154Application of Abstract FactoryInterface ofInterface of Abstract Factory Abstract Factory Applied to Word Applied to Word ProcessorProcessorClientSmallStyleLargeStyleStyleDocumen

14、t setStyle() display(). . . . . . .1More Detailed?More Detailed?集合体Ensemble角色18TP-154“ “Word ProcessorWord Processor” ” Interaction Interaction风格的确定依赖于用户输入19TP-154Detailed SolutionDetailed SolutionnMain(), get Title, Heading, Text from users input;nCreate Document Object-documentnStyle SelectionnSet

15、 Parameters to document.nInteracting with the Object.nMaybe: document.display()20TP-154Abstract FactoryAbstract FactoryApplied to Word Applied to Word ProcessorProcessorDocument getAHeading() getATitle() getDocumentFromUser()createSmallStyle getAHeading() getATitle()LargeStyle getAHeading() getATitl

16、e()Title valueHeading valueLargeHeading display()SmallHeading display()LargeTitle display()SmallTitle display()Text value10n 0nStyle getAHeading() getATitle()DisplayableDisplayable display()Client getStyleFromUser() displayDocument()style21TP-154Abstract Factory: Narrow InterfaceAbstract Factory: Narrow InterfaceStyle.AbstractFactory getAPart1Object() getAPart2Object()StyleAFactoryStyleBFactorya

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号