Decorate Pattern 装饰者模式
装饰模式的定义:
动态地将责任附加到对象向,若要扩展功能,装饰模式提供了比继承更有弹性的替代方案。
遵循的设计原则是开闭原则,也是对扩展开放,对修改关闭。
下面是类图
示例代码
/**
*定义被装饰者
*
*/
public interface Human { public void wearClothers(); public void walkToWhere();
} /**
* 定义抽象装饰者
*
*/
public abstract class Decorate implements Human { Human human; public Decorate(Human human) {
this.human = human;
} public void wearClothers() {
this.human.wearClothers();
} public void walkToWhere() {
this.human.walkToWhere();
}
} /**
*
*装饰者 zero
*/
public class DecorateZero extends Decorate{ public DecorateZero(Human human) {
super(human);
} @Override
public void walkToWhere() {
super.walkToWhere();
System.out.println("Zero.............walkToWhere");
} @Override
public void wearClothers() {
super.wearClothers();
System.out.println("Zero.............wearClothers");
}
} /**
* 装饰者two
*
*/
public class DecorateTwo extends Decorate{ public DecorateTwo(Human human) {
super(human);
}
@Override
public void walkToWhere() {
super.walkToWhere();
System.out.println("Two.............walkToWhere");
} @Override
public void wearClothers() {
super.wearClothers();
System.out.println("Two.............wearClothers");
}
} //
public class DecorateFirst extends Decorate{ public DecorateFirst(Human human) {
super(human);
} @Override
public void walkToWhere() {
super.walkToWhere();
System.out.println("First.............walkToWhere");
} @Override
public void wearClothers() {
super.wearClothers();
System.out.println("First.............wearClothers");
}
} /**
*
*测试代码
*/
public class Test { public static void main(String[] args) {
DecorateZero zero=new DecorateZero(new DecorateFirst(new DecorateTwo(new Human() {
//这里装饰者原有的行为,这里我没有具体去写实现类。
@Override
public void wearClothers() {
// TODO Auto-generated method stub
System.out.println("1111111wearClothers1111111"); } @Override
public void walkToWhere() {
// TODO Auto-generated method stub
System.out.println("1111111walkToWhere1111111");
}
})));
zero.walkToWhere();
System.out.println("*******************************************");
zero.wearClothers();
}
} //测试结果
1111111walkToWhere1111111
Two.............walkToWhere
First.............walkToWhere
Zero.............walkToWhere
*******************************************
1111111wearClothers1111111
Two.............wearClothers
First.............wearClothers
Zero.............wearClothers
关键点:
1、Decorator抽象类中,持有Human接口,方法全部委托给该接口调用,目的是交给该接口的实现类即子类进行调用。
2、Decorator抽象类的子类(具体装饰者),里面都有一个构造方法调用super(human),这一句就体现了抽象类依赖于子类实现即抽象依赖于实现的原则。因为构造里面参数都是Human接口,只要是该Human的实现类都可以传递进去,即表现出Decorator dt = new Decorator_second(new Decorator_first(new
Decorator_zero(human)));这种结构的样子。所以当调用dt.wearClothes();dt.walkToWhere()的时候,又因为每个具体装饰者类中,都先调用super.wearClothes和super.walkToWhere()方法,而该super已经由构造传递并指向了具体的某一个装饰者类(这个可以根据需要调换顺序),那么调用的即为装饰类的方法,然后才调用自身的装饰方法,即表现出一种装饰、链式的类似于过滤的行为。
3、具体被装饰者类,可以定义初始的状态或者初始的自己的装饰,后面的装饰行为都在此基础上一步一步进行点缀、装饰。
4、装饰者模式的设计原则为:对扩展开放、对修改关闭,这句话体现在我如果想扩展被装饰者类的行为,无须修改装饰者抽象类,只需继承装饰者抽象类,实现额外的一些装饰或者叫行为即可对被装饰者进行包装。
装饰Java.io类
java.io包内的类太多了,简只是……”排山倒海“,其实是因为里面许多类都是装饰者,其实java.io也引出装饰者模式的一个缺点:利用装饰着模式,常常设计中有大量的小类,数量实在太多,造成使用者非常大的困扰。了解装饰者模式的原理看起就会简单多了。
编写自己的java.io装饰者
功能:把输入的大写字母写成小写
例如:This is LONGHAIBO,使用此装饰者变成this is longhaibo
public class TestDecorate { public static void main(String[] args) { int c;
try (InputStream in = new LowerCaseIputStream(new BufferedInputStream(
new FileInputStream("src/decorate.txt")))) {
while ((c=in.read())>0) {
System.out.print((char)c);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} }
} /**
* Java io 装饰者模式
* 功能:把输入的大写字母写成小写
* 例如:This is LONGHAIBO,使用此装饰者变成this is longhaibo
*
*/
class LowerCaseIputStream extends FilterInputStream { protected LowerCaseIputStream(InputStream in) {
super(in);
}
//针对字节
@Override
public int read() throws IOException {
int c=super.read();
return (c==-1?c:Character.toLowerCase((char)c));
}
//针对字节数组
@Override
public int read(byte[] b, int off, int len) throws IOException {
int result=super.read(b, off, len);
for (int i = off; i < off+result; i++) {
b[i]=(byte)Character.toLowerCase((char)b[i]);
}
return result;
} }
输出结果
this is longhaibo
参考资料
head_first设计模式(中文版)
http://blog.csdn.net/jason0539/article/details/22713711
Decorate Pattern 装饰者模式的更多相关文章
- Decorator Pattern (装饰者模式)
装饰者模式( Decorator Pattern ) 意图 : 动态的给一个对象添加一些额外的功能,IO这块内容体现出了装饰模式,Decorator模式相比生成子类更为灵活. 角色 : 1)抽象构件角 ...
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- java之装饰器模式
Decorator Pattern(装饰器模式),定义:Attach additional responsibilities to an object dynamically. Decorators ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
- C#设计模式之装饰者模式(Decorator Pattern)
1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...
- java_设计模式_装饰者模式_Decorator Pattern(2016-07-28)
装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式的结构 装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任.换言之,客户 ...
- 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法
装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...
- 设计模式 - 装饰者模式(Decorator Pattern) 具体解释
装饰者模式(Decorator Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033 装饰者 ...
- 设计模式学习--装饰者模式(Decorator Pattern)
概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
随机推荐
- DEV GridControl小结
1. 如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 2. 如何新增一条记录 (1).gridView.Ad ...
- 微信h5页面禁止下拉露出网页来源
1.可以给document的touchmove事件禁止掉就行了 document.querySelector('body').addEventListener('touchmove', functio ...
- 关于html转pdf
本篇作为一个备忘,记录一下html转pdf中遇到的问题. 没有具体做这个东西的时候不知道,网络上能将html转换成pdf的资源并不太多,使用上有很多都存在一些问题.能完美将一个站点中的页面(这里说一个 ...
- NE Upgrade python script. Need to write a Tkinter GUI for it
# -*- coding: utf-8 -*-#from ftplib import FTP __authour__='CC' import osimport telnetlibimport time ...
- 28.zookeeper单机(Standalones模式)和集群搭建笔记
zookeeper单机(Standalones模式)和集群搭建: 前奏: (1).zookeeper也可以在windows下使用,和linux一样可以单机也可以集群,具体就是解压zookeeper-3 ...
- Failed to load resource: the server responded with a status of 500 (Internal Server Error)
错误提示: 原因: MIME类型错误. 之前添加json.woff.woff2映射,更换系统(Win7升Win10)后配置失效,在webconfig中删除映射即可,因为Win10自带上面3个MIME映 ...
- php : Warning: strftime(): It is not safe to rely on the system's timezone settings.
使用 Smarty 的时候出现这种警告: Warning: strftime(): It is not safe to rely on the system's timezone settings. ...
- php curl_init函数用法
使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓取的网 页,然后就可以以程序的方式得到你想要的数据了.无论是你想从从一个链接上取部分数据,或是取一个XML文件并 ...
- Wiki设置
在Wiki安装完成后,就wiki会提示下载LocalSettings.php文件,这是wiki的设置文件,当我们要对wiki进行设置的时候,就需要用到这个文件. 下面对常用的操作设置做简要讲解: —— ...
- Redis教程(四):Hashes数据类型
转自:http://www.jb51.net/article/65240.htm 一.概述: 我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器 ...