设计模式 (十)装饰模式(Decorator).doc_第1页
设计模式 (十)装饰模式(Decorator).doc_第2页
设计模式 (十)装饰模式(Decorator).doc_第3页
设计模式 (十)装饰模式(Decorator).doc_第4页
设计模式 (十)装饰模式(Decorator).doc_第5页
全文预览已结束

下载本文档

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

文档简介

设计模式 (十)装饰模式(Decorator) 装饰模式(Decorator)就是使用被装饰的一个子类的实例,在客户端将这个子类的实例委托给装饰类。装饰模式是结成关系的一个替代方案。简单的装饰模式是原理图入下:装饰模式以对客服端透明的方式增添了对象的功能,其在与动态的给对象添加了责任,当然这里的继承便是静态的。其中重要的地方时装饰对象和真是对象有相同的接口,这样客户端就可以和真是对象一样的交互方式和装饰对象交互,然后装饰对象把所有从客户端接收过来的请求全部转发给真是对象,然后在返还给客户端,这样装饰对象就可以再转发前或者以后添加一些附加功能而不影响对真是对象的操作,这样在不改变原有类的基础想,可以实现对于原有类的这种额外功能的实现,增强了程序的复用性。同时装饰模式比继承好的地方就是,装饰模式可以动态的对已经存在的类进行任意的组合,实现想要的功能,而继承是静态的实现,不能改变原有类的实现,如果要添加更多的功能,只有添加更多的派生类来实现,这个简洁在下面的例子里对于最后一次打印输出就有很明显的效果,定义好了两个装饰类以后,不用再定义第三个就可以实现两个装饰类排列组合的效果。下面就简单的做了一个通过对于手机接电话的一个扩展,接听电话之前有一个彩铃,接受电话之后会回拨一段广告,这样就出来了下面的例子:javaview plaincopy1. packagecom.designpattern.decorator;2. 3. publicinterfacePhone4. publicvoidrecevieCall(Stringname);5. javaview plaincopy1. packagecom.designpattern.decorator;2. 3. publicclassChinaMobileimplementsPhone4. 5. Override6. publicvoidrecevieCall(Stringname)7. System.out.println(steprecevie+name+call);8. 9. 10. javaview plaincopy1. packagecom.designpattern.decorator;2. 3. publicabstractclassDecoratorimplementsPhone4. 5. privatePhonephone;6. 7. publicDecorator(Phonephone)8. this.phone=phone;9. 10. 11. Override12. publicvoidrecevieCall(Stringname)13. this.phone.recevieCall(name);14. 15. 16. javaview plaincopy1. packagecom.designpattern.decorator;2. 3. publicclassRingBeforeChinaMobileDecoratorextendsDecorator4. 5. publicRingBeforeChinaMobileDecorator(Phonephone)6. super(phone);7. 8. 9. Override10. publicvoidrecevieCall(Stringname)11. System.out.println(stepringbeforerecevie+name+call);12. super.recevieCall(name);13. 14. 15. javaview plaincopy1. packagecom.designpattern.decorator;2. 3. publicclassAdAfterChinaMobileDecoratorextendsDecorator4. 5. publicAdAfterChinaMobileDecorator(Phonephone)6. super(phone);7. 8. 9. Override10. publicvoidrecevieCall(Stringname)11. super.recevieCall(name);12. System.out.println(stepadafterrecevie+name+call);13. 14. 15. javaview plaincopy1. packagecom.designpattern.decorator;2. 3. publicclassClient4. publicstaticvoidmain(Stringargs)5. Phonephone=newChinaMobile();6. Decoratordecorator=newRingBeforeChinaMobileDecorator(phone);7. decorator.recevieCall(andimuise);8. System.out9. .println(*);10. 11. decorator=newAdAfterChinaMobileDecorator(phone);12. decorator.recevieCall(halberd);13. System.out14. .println(*);15. 16. decorator=newRingBeforeChinaMobileDecorator(17. newAdAfterChinaMobileDecorator(phone);18. decorator.recevieCall(teacher);19. System.out20. .println(*);21. 22. 最终输出结果为,很明显在第三次输出的时候我想同时实现两种效果,就把另一个装饰类为为了其中一个装饰类的属性,因为无论是装饰类还是真实类他们是实现的共同的接口,这样就对实现提供了很好的效果htmlview plaincopy1. stepringbeforerecevieandimuisecall2. steprecevieandimuisecall3. *4. stepreceviehalberdcall5. stepadafterreceviehalberdcall6. *7. stepringbeforereceviete

温馨提示

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

评论

0/150

提交评论