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): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
随机推荐
- Android项目架构之业务组件化
前言: 从个人经历来说的话,从事APP开发这么多年来,所接触的APP的体积变得越来越大,业务的也变得越来越复杂,总来来说只有一句话:这是一个APP臃肿的时代!所以为了告别APP臃肿的时代,让我们进入一 ...
- C#数组
数组是一种数据结构,它包含若干相同类型的变量.数组是使用类型声明的: type[] arrayName; 下面的示例创建一维.多维和交错数组: class TestArraysClass { stat ...
- 我的sql 记录
----------2017-01-03 21:56:56---------- --字符串分隔 start------------- use LDSQL GO CREATE function [dbo ...
- memcached源码安装(linux和windows)
如果是在windows环境下编译安装,我这边是基于cygwin或msys2方式 安装cygwin环境,http://www.cnblogs.com/skey_chen/p/5765179.html 安 ...
- Dreamweaver架设网站终极教程
转自:http://www.cnblogs.com/loveme513/archive/2006/04/03/365969.html 一.定义站点 1. 在任意一个根目录下创建好一个文件夹(我们这里假 ...
- [转]Meta http-equiv属性详解
http-equiv顾名思义,相当于http的文件头作用,它可以向浏览器传回一些有用的信息,以帮助正确和精确地显示网页内容,与之对应的属性值为content,content中的内容其实就是各个参数的变 ...
- 浅析word-break work-wrap区别
word-break:[断词] 定义:规定自动换行的处理方法. 注:通过word-break使用,可以实现让浏览器在任意位置换行. 语法:word-break: normal|break-all| ...
- jquery总结06-动画事件02-上卷下拉动画
.slideDown() 下拉动画 动画执行之后的操作写在回调函数里 $("ele").slideDown(1000, function() { //等待动画执行1秒后,执行别的动 ...
- Polly
Polly Polly is a .NET 3.5 / 4.0 / 4.5 / PCL (Profile 259) library that allows developers to express ...
- Comparator
1.java.util.Comparator 一个实现了comparator接口的对象能够对比不同的对象.它不能用于同一个类的不同实例的对比,但是可以用于其他的类的实例做对比.必须实现java.uti ...