欢迎来到天天文库
浏览记录
ID:51968220
大小:500.00 KB
页数:64页
时间:2020-03-26
《软件设计模式-体系结构-PPT讲稿 0-Software-Design-Patterns-20121030 lec1_FactoryMethod-Creational.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Lecture1TheFactoryMethodPattern工厂方法模式有的工厂生产飞机,有的工厂生产汽车,听说过有生产类的对象的工厂吗?本讲讲述的工厂方法模式专门生产对象。1ContentsofthislectureIntroductionofthefactorymethodpatternTheoryofthesimplefactorymethodPatternanddesignexamplesTheoryofthefactorymethodpatternanddesignexamplesFurtherdiscus
2、sionofthefactorymethodpattern2IntroductionoftheFactoryMethodPattern3IntroductiontoFactoryMethodPattern问题:怎样调用类中的一个方法?Howtoinvokeamethodinaclass?Ifanapplicationobjectknowstheexactmethoditneeds,itcandirectlyinstantiatetheclassfromtheclasshierarchythatcontainstheme
3、thod.Seeexamplebelow.4Car+buyCar()+buyParts()Lincoln+buyCar()+buyParts()Cadillac+buyCar()+buyParts()Buick+buyCar()+buyParts()ClientobCar:CarMain()ClientObjectDirectlyAccessingaClassHierarchyIntroductiontoFactoryMethodPatternCarmyCar;myCar=newLincoln();myCar.buyC
4、ar();5新的情况:1)如果一个类的结构有很多子类2)你不知道该准确地调用哪一个怎么办呢???IntroductiontoFactoryMethodPattern6Question:Howtoinvokeamethodinaclasshierarchythatcontainsmanysubclassesandyoudon’tknowexactlywhichonetoselect?Thechoicemaydependon:运行状态.Thestateoftherunningapplication(inputfromuse
5、rs)应用配置设置.ApplicationconfigurationsettingsInsuchcases,anapplicationobjectneedstouseconditionalstatementstoinstantiateanappropriateclassfromthehierarchytoaccessitsservicesIntroductiontoFactoryMethodPattern7Anapplicationobjectneedstoimplementtheclassselectioncrite
6、riatoinstantiateanappropriateclassfromthehierarchytoaccessitsservicesClientobCar:CarMain()If(usrChoice==1){obCar=newLincoln(model);obCar.buyCar();}elseif(usrChoise==2){obCar=newCadillac(model)obCar.buyCar();}Car+buyCar()+buyParts()Lincoln+buyCar()+buyParts()Cadi
7、llac+buyCar()+buyParts()Buick+buyCar()+buyParts()IntroductiontoFactoryMethodPattern8该设计的缺点:Thistypeofdesignhasthefollowingdisadvantages:Highcoupling(高耦合).Becauseeveryapplicationobjectthatintendstousetheservicesofferedbytheclasshierarchyneedstoimplementtheclassse
8、lectioncriteria,itresultsinahighdegreeofcouplingbetweenanapplicationobjectandtheserviceproviderclasshierarchy.IntroductiontoFactoryMethodPattern9Inelegantstatement(难看
此文档下载收益归作者所有