设计模式——装饰模式
2017-02-27 12:18
260 查看
装饰的意思想必大家也都知道,就是起修饰美化作用的物品,比如装修效果图造型的轮廓和雕刻装饰;在身体或物体的表面加些附属的东西,使之更美观。在应用程序里面装饰模式也是很有用武之地的。装饰模式的职责:动态的为一个对象添加新的功能。装饰模式是一种用于代替继承的技术,没有必要通过继承增加子类就能扩展对象的新功能。使用对象的关联关系代替继承关系,更加灵活,同时避免类型体系的快速膨胀。
装饰模式的适用性:
1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
2.处理那些可以撤销的职责。
3.当不能采用生成子类的方法进行扩充时。
比如说我们的手机,以前最新出来的手机也就是打电话功能,在发展的过程中逐渐增加了短信、视频、音乐、聊天等功能。
下面我们就以手机的功能来实现一下功能
测试代码
实现效果
将测试代码里面的参数稍微的切换一些试试看,会有什么样的效果
运行效果图
其实我们的测试代码还可以再次稍微的切换一下
运行效果图
通过上面的案例你就会发现我们新增加的每一个功能都是动态的透明的添加的,最后一次的测试代码的样式是不是很熟悉,我们平常使用的IO中输入流和输出流就是这样的设计。到这里装饰功能也就基本结束了,谢谢浏览,如有疑问欢迎留言。
装饰模式的适用性:
1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
2.处理那些可以撤销的职责。
3.当不能采用生成子类的方法进行扩充时。
比如说我们的手机,以前最新出来的手机也就是打电话功能,在发展的过程中逐渐增加了短信、视频、音乐、聊天等功能。
下面我们就以手机的功能来实现一下功能
public interface MobilePhone { String function (); } class TelephoneMobilePhone implements MobilePhone{ @Override public String function() { return "电话功能"; } } /** * 具体的手机对象 */ class ConcreteMobilePhone implements MobilePhone{ private MobilePhone mobilePhone; public ConcreteMobilePhone(MobilePhone mobilePhone) { super(); this.mobilePhone = mobilePhone; } @Override public String function() { return mobilePhone.function(); } } /** * 增加短信功能 */ class MessageMobilePhone extends ConcreteMobilePhone{ public MessageMobilePhone(MobilePhone mobilePhone) { super(mobilePhone); } @Override public String function() { System.out.println(message()); return super.function(); } public String message() { return "发短信功能"; } } /** * 增加音乐功能 */ class MusicMobilePhone extends ConcreteMobilePhone{ public MusicMobilePhone(MobilePhone mobilePhone) { super(mobilePhone); } @Override public String function() { System.out.println(music()); return super.function(); } public String music() { return "音乐功能"; } } /** * 增加视频功能 */ class VideoMobilePhone extends ConcreteMobilePhone{ public VideoMobilePhone(MobilePhone mobilePhone) { super(mobilePhone); } @Override public String function() { System.out.println(playVideo()); return super.function(); } public String playVideo(){ return "视频功能"; } } /** * 增加聊天功能 */ class ChatMobilePhone extends ConcreteMobilePhone{ public ChatMobilePhone(MobilePhone mobilePhone) { super(mobilePhone); } @Override public String function() { System.out.println(chat()); return super.function(); } public String chat() { return "聊天功能"; } }
测试代码
TelephoneMobilePhone tPhone = new TelephoneMobilePhone(); System.out.println(tPhone.function()); System.out.println("《《-------新增功能--------》》"); MessageMobilePhone mPhone = new MessageMobilePhone(tPhone); System.out.println(mPhone.function()); System.out.println("《《-------新增功能--------》》"); MusicMobilePhone musicMobilePhone = new MusicMobilePhone(tPhone); System.out.println(musicMobilePhone.function()); System.out.println("《《-------新增功能--------》》"); VideoMobilePhone vPhone = new VideoMobilePhone(tPhone); System.out.println(vPhone.function()); System.out.println("《《-------新增功能--------》》"); ChatMobilePhone cPhone = new ChatMobilePhone(tPhone); System.out.println(cPhone.function());
实现效果
将测试代码里面的参数稍微的切换一些试试看,会有什么样的效果
TelephoneMobilePhone tPhone = new TelephoneMobilePhone(); System.out.println(tPhone.function()); System.out.println("《《-------新增功能--------》》"); MessageMobilePhone mPhone = new MessageMobilePhone(tPhone); System.out.println(mPhone.function()); System.out.println("《《-------新增功能--------》》"); MusicMobilePhone musicMobilePhone = new MusicMobilePhone(mPhone); System.out.println(musicMobilePhone.function()); System.out.println("《《-------新增功能--------》》"); VideoMobilePhone vPhone = new VideoMobilePhone(musicMobilePhone); System.out.println(vPhone.function()); System.out.println("《《-------新增功能--------》》"); ChatMobilePhone cPhone = new ChatMobilePhone(vPhone); System.out.println(cPhone.function());
运行效果图
其实我们的测试代码还可以再次稍微的切换一下
TelephoneMobilePhone tPhone = new TelephoneMobilePhone(); System.out.println(tPhone.function()); System.out.println("《《-------新增功能--------》》"); ChatMobilePhone cPhone = new ChatMobilePhone(new MessageMobilePhone(new MusicMobilePhone(new VideoMobilePhone(tPhone)))); System.out.println(cPhone.function());
运行效果图
通过上面的案例你就会发现我们新增加的每一个功能都是动态的透明的添加的,最后一次的测试代码的样式是不是很熟悉,我们平常使用的IO中输入流和输出流就是这样的设计。到这里装饰功能也就基本结束了,谢谢浏览,如有疑问欢迎留言。
相关文章推荐
- 设计模式---装饰模式
- NET设计模式之一:装饰模式(Decorator Pattern)
- 设计模式中结构型模式(四)装饰模式(Decorator)
- 设计模式c#描述——装饰(Decorator)模式
- 第四个设计模式:装饰模式
- 设计模式c#语言描述——装饰(Decorator)模式
- 结构型设计模式---Decorator模式(装饰模式)
- .NET设计模式研究之装饰模式
- 解读设计模式----装饰模式(Decorator Pattern)
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
- 用实例解说Dot Net设计模式——装饰模式
- 第四个设计模式:装饰模式
- AspectJ实现设计模式(七)—装饰模式
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
- 设计模式之---装饰(decorator)
- 设计模式c#语言描述——装饰(Decorator)模式
- 设计模式之装饰模式篇(Decorator)
- 设计模式之--装饰器模式的硬编码的静态装饰和链化
- 设计模式学习笔记-装饰模式
- 设计模式(11)-装饰模式(Decorator)