设计模式总结final(大连东软信息学院期末复习).doc_第1页
设计模式总结final(大连东软信息学院期末复习).doc_第2页
设计模式总结final(大连东软信息学院期末复习).doc_第3页
设计模式总结final(大连东软信息学院期末复习).doc_第4页
设计模式总结final(大连东软信息学院期末复习).doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

设计模式总结final(大连东软信息学院期末复习) 1.单例模式(票务系统) (1)类图 (2)主要解决问题?当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时?当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展实例时 (3)CATM系统中的例子?Controller类 (4)范例代码Singleton-anInstance:Singleton-Singleton()+getAnInstance():Singleton packagesingleton;public classSingletonprivate staticSingleton anInstance;private Singleton()public staticSingleton getAnInstance()if(anInstance=null)anInstance=new Singleton();return anInstance;2.工厂模式a.工厂方法模式(多个厂家生产一个产品) (1)类图 (2)主要解决问题?当一个类不知道他所创建的对象的类的时候?当一个类希望由它的子类来指定它所创建的对象的时候?当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候 (3)CATM系统中的例子?Controller类中的getAnInstance()方法,即是一个简单的工厂方法。 单个方法ClientFactory+produce():ProductProductConcreteProduct1ConcreteProduct2public classFarmerpublic Fruitproduce(String msg)Fruit fruit=null;if(msg.equals(apple)fruit=new Apple();else if(msg.equals(grape)fruit=new Grape();return fruit;多个方法ClientFactory+produce1():Product+produce2():ProductProductConcreteProduct1ConcreteProduct2public classFarmerpublic Fruit produceApple()return newApple();public Fruit produceGrape()return newGrape();静态方法ClientFactory+produce1():Product+produce2():ProductProductConcreteProduct1ConcreteProduct2public classFarmerpublic staticFruitproduceApple()return newApple();public staticFruitproduceGrape()return newGrape();b.抽象工厂模式(多个工厂生产多个产品) (1)类图ClientAbstractFactoryProduct+produce():ProductConcreteFactory1ConcreteFactory2ConcreteProduct1ConcreteProduct2public interfaceFarmerpublic Fruitproduce();public classAppleFarmer implementsFarmerpublic Fruitproduce()return newApple();public classGrapeFarmer implementsFarmerpublic Fruitproduce()return newGrape(); (2)主要解决问题?一个系统独立于它的产品的创建、组合和表示时?一个系统要由多个产品系列中的一个来配置时?当你要强调一系列相关的产品对象的设计一边进行联合使用时?当你提供一个产品类库,而只想显示它们的接口的时候而不是实现的时候3.观察者模式 (1)类图Observer+update()Subject+register()+remove()+notifyObservers()ConcreteObserver1+update()ConcreteObserver2+update()ConcreteSubject1ConcreteSubject2package observer;public interfaceDeamonpublic voidupdate(int state);package observer;public classLion implementsDeamonpublic voidupdate(int state)System.out.println(Lion:+state);package observer;public classTiger implementsDeamonpublic voidupdate(int state)System.out.println(Tiger:+state);package observer;import java.util.ArrayList;import java.util.List;public abstractclass Treasureprivate Listdeamons=new ArrayList();protected int state;public voidsetState(int state)this.state=state;this.notifyDeamons();public voidregister(Deamon deamon)deamons.add(deamon);public voidremove(Deamon deamon)deamons.remove(deamon);public voidnotifyDeamons()for(Deamon deamon:deamons)deamon.update(state);package observer;public classGold extendsTreasurepublic Gold(int state)this.state=state;package observer;public classDiamond extendsTreasurepublic Diamond(intstate)this.state=state; (2)主要解决问题?当一个抽象模型有两个方面的,其中一个方面依赖与另一个方面。 ?当一个对象的改变需要同时改变其他对象,而不知道具体有多对象有待改变?当一个对象必须通知其他对象,而它又不能假定其他对象是谁。 (3)CATM系统中的例子?model.CoinObserver?model.MaterialObserver4.策略模式(涉及到角色和算法) (1)类图Weapon+fight()Knight+fight()SwordKnife package strategy;public classKnightprivate Weapon weapon;public voidsetWeapon(Weaponweapon)this.weapon=weapon;public voidfight()weapon.fight();package strategy;public interfaceWeaponpublic voidfight();package strategy;public classSword implementsWeaponpublic voidfight()System.out.println(Sword bewith you!);package strategy;public classKnife implementsWeaponpublic voidfight()System.out.println(Knife bewith you!);packagestrategy;public classKnightTesterpublic staticvoid main(Stringargs)Knight tom=new Knight();tom.setWeapon(new Sword();tom.fight();tom.setWeapon(new Knife();tom.fight(); (2)主要解决问题?许多相关的类仅仅是行为有异,“策略”提供一种用多行为中的一个行为来配置一个类的方法;?需要使用一个算法的不同变体?算法使用客户不应该知道的数据,使用策略模式将算法相关的数据结构隐藏起来?一个类定义多种行为,并且将这些行为在这个的操作中以多个条件语句的形势出现。 将相关的条件分支移入他们各自的Stragety类中代替这些条件语句 (3)CATM系统中的例子?StateController5.状态模式(一个对象状态不同行为不同) (1)类图 (2)主要解决问题?当状态数目不是很多的时候,Switch/Case可能可以搞定。 但是当状态数目很多的时候(实际系统中也正是如此),维护一大组的Switch/Case语句将是一件异常困难并且容易出错的事情。 ?状态逻辑和动作实现没有分离。 在很多的系统实现中,动作的实现代码直接写在状态的逻辑当中。 这带来的后果就是系统的扩展性和维护得不到保证。 ?State模式就是被用来解决上面列出的两个问题的,在State模式中我们将状态逻辑和动作实现进行分离。 当一个操作中要维护大量的case分支语句,并且这些分支依赖于对象的状态。 State模式将每一个分支都封装到独立的类中。 (3)CATM系统中的例子?State类 (3)范例代码Sleepingwake()Walkingsleep()goUp()RiddiinggetDown()Knight+SLEEP+WALK+RIDE+sleep()+wake()+walk()+goUp()+ride()+getDown()+fight()+chang2Sleep()+change2Walk()+change2Ride()State+sleep()+wake()+walk()+goUp()+ride()+getDown()+fight()+setKnight(knight:Kinght)Sleeping WalkingRidding package state;public classKnightprivate StatetheCurrentState;private staticfinal StateSLEEP=new Sleeping();private staticfinal StateWALK=new Walking();private staticfinal StateRIDE=new Ridding();public Knight()theCurrentState=SLEEP;SLEEP.setKnight(this);WALK.setKnight(this);(Ridding)RIDE).setKnight(this);public voidsleep()theCurrentState.sleep();public voidwake()theCurrentState.wake();public voidwalk()theCurrentState.walk();public voidgoUp()theCurrentState.goUp();public voidride()theCurrentState.ride();public voidgetDown()theCurrentState.getDown();public voidfight()theCurrentState.fight();public voidchang2Sleep()theCurrentState=SLEEP;public voidchang2Walk()theCurrentState=WALK;public voidchang2Ride()theCurrentState=RIDE;package state;public interfaceStatepublic voidsleep();public voidwake();public voidwalk();public voidgoUp();public voidride();public voidgetDown();public voidfight();public voidsetKnight(Knight knight);packagestate;public classSleeping implementsStateprivate Knightknight;public voidsetKnight(Knight kinght)this.knight=knight;public voidsleep()System.out.println(Donot interruptme!);public voidwake()System.out.println(It istime toWake!);knight.chang2Walk();public voidwalk()System.out.println(Sorry!I amsleeping!);public voidgoUp()System.out.println(Sorry!I amsleeping!);public voidride()System.out.println(Sorry!I amsleeping!);public voidgetDown()System.out.println(Sorry!I amsleeping!);public voidfight()System.out.println(Sorry!I amsleeping!);6.模板方法模式 (1)类图 (2)主要解决问题?一次性实现一个算法的不变部分,并将可变的行为留给子类实现?各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复?控制子类扩展 (3)CATM系统中的例子?Beverage类4)范例Template+templateMethod()#step1()#step2()Realization1+step1()+step2()Realization2+step1()+step2()package templatemethod;public abstractclass Decorderpublic voidrecordBreaker()String mitext=read();String mingtext=decode(mitext);send(mingtext);private Stringread()return!*&()&*;private voidsend(String mingtext)System.out.println(mingtext);protected abstractString decode(String mitext);package templatemethod;public classRSADecorder extendsDecorderOverride protectedString decode(String mitext)return$+mitext+$;package templatemethod;public classMD5Decorder extendsDecorderOverride protectedString decode(String mitext)return#+mitext+#;7.命令模式(参数,日志) (1)类图 (2)主要解决问题?抽象出待执行的动作以参数化某对象?在不同的时刻,指定、排列和执行请求?支持取消操作?支持修改日志 (3)CATM系统中的例子?Command接口8.装饰器模式 (1)类图 (2)主要解决问题?在不影响其他对象的情况下,以动态、透明的方式给单个对象添加责任?处理那些可以撤销的职责?当不能采用生成子类的方法进行扩时。 一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。 另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。 (3)CATM系统中的例子?Decrator类与Command接口 (4)Beveragedescription:StringgetDescription()cost()#baseCondimentsgetDescription()SoyDecafDecaf()cost()SteamedMilkSteamedMilk()getDescription()cost()DarkRoastDarkRoast()cost()Soy()getDescription()cost()HouseBlendHouseBlend()cost()EspressoEspresso()cost()MochaMocha()getDescription()cost()WhipWhip()getDescription()cost()package decorator;public classBeveragepublic StringgetDescription()returnbeverage;public intcost()return0;package decorator;public classEspresso extendsBeveragepublic StringgetDescription()returnespresso;publi

温馨提示

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

评论

0/150

提交评论