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): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
随机推荐
- 某预约系统分析 > 某区公共自行车租车卡在线预约,关于如何提高成功概率
概诉 网上提交预约申请单,线下面交完成实体卡的交付和办理. 本文主要从技术角度分析预约页面,仅供初学者技术交流使用. 表单输入和校验 系统通过2步的确认点击到达信息输入页面. 地址:/bjggzxc/ ...
- ATS
一) 起因:由于苹果规定2017年1月1日以后,所有APP都要使用HTTPS进行网络请求. 之前,可以在Info.plist中禁用ATS功能,按如下设置即可. <key>NSAppTran ...
- Cheatsheet: 2016 10.01 ~ 10.31
Docker Introduction to Docker Monitoring Database MongoDB: The Good, The Bad, and The Ugly Web 4 Key ...
- Python -- 数据加载、存储与文件格式
标签(空格分隔): Python 读入读出通常可以划分为几个大类:读取文本文件和其他更高效的磁盘存储格式,加载数据库中的数据,利用Web API操作网络资源. 读写文本格式的数据 pandas提供了一 ...
- PHP---Mysql常用语法(增删改查)
1.数据库操作: 创建数据库:create databas ...
- mysql中文乱码问题解决
在mysql5.0以上,中文乱码不能用网上的大多数方法,我用网上改编码的问题,让我mysql服务器再也起不起来了. 我用的mysql5.7.16绿色免安装版. 在根目录下的配置文件里my.ini(或者 ...
- [git] git代理及常用命令,远程桌面代理
1.代理 公司只能内网,上外网只能用代理,坑货! 2. 更新代码命令 1)下载代码:git clone ------------ 2) 指定目录: cd 文件名 3)git add ...
- 利用Object.prototype.toString方法,实现比typeof更准确的type校验
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...
- Virtualbox安装USB2.0/3.0
系统:Ubuntu16.04 软件:Virtualbox5.1 1.打开Virtualbox,不启动虚拟系统. 2.点击设置->USB->启动usb2.0. 3.若发现不能启用,则到官网下 ...
- Python下载进度条
import dialog,e32,appuifw def ru(x):return x.decode('utf8') def qu(): appuifw.note(ru('已取消')) wait.c ...