装饰者模式

动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。

具体被装饰者和抽象装饰类都继承于抽象被装饰者类,继承的是类型,而不是行为。行为来自装饰者和基础组件,或与其他装饰者之间的组合关系。

装饰模式的角色

  抽象构件角色(Component):给出一个抽象接口,以规范准备接收附加责任的对象。

  具体构件角色(Concrete Component):定义将要接收附加责任的类。

  装饰角色(Decorator):持有一个构件(Component)对象的引用,并定义一个与抽象构件接口一致的接口。

  具体装饰角色(Concrete Decorator):负责给构件对象“贴上”附加的责任。

装饰模式的特点

  装饰对象和真实对象有相同的接口。这样客户端对象就可以以和真实对象相同的方式和装饰对象交互。

  装饰对象包含一个真实对象的引用(reference)。

  装饰对象接收所有来自客户端的请求,它把这些请求转发给真实的对象。

  装饰对象可以在转发这些请求之前或之后附加一些功能。

  这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。

代码

 public class Zhuangxiuzhe {
public static void main(String[] args) {
Zhuangxiuzhe zhuangxiuzhe=new Zhuangxiuzhe();
Component component=zhuangxiuzhe.new ConcreteComponent();
Component decorator=zhuangxiuzhe.new ConcreteDecorator1(component);
decorator.doMethod();
Component decorator2=zhuangxiuzhe.new ConcreteDecorator2(decorator);
decorator2.doMethod(); } interface Component { public void doMethod(); } class ConcreteComponent implements Component { public void doMethod() {
System.out.println("一般操作"); } }
abstract class Decorator implements Component{ protected Component component;
public Decorator(Component component){
this.component=component;
} }
class ConcreteDecorator1 extends Decorator{ public ConcreteDecorator1(Component component) {
super(component); }
public void test1(){
System.out.println("ConcreteDecorator1 额外操作");
} public void doMethod() {
super.component.doMethod();
test1();
}
}
class ConcreteDecorator2 extends Decorator{ public ConcreteDecorator2(Component component) {
super(component); } public void doMethod() {
super.component.doMethod();
test2();
} public void test2(){
System.out.println("ConcreteDecorator2 额外操作");
}
} }

执行结果

一般操作
ConcreteDecorator1 额外操作
一般操作
ConcreteDecorator1 额外操作
ConcreteDecorator2 额外操作


突然发现装修者模式跟责任链模式有点相同,自身都引用了抽象类,这样可以调用传入的包装类

区别在于功能上:责任链强调的是请求由谁来处理,而装修者模式则是对引入的对象的现有功能进行包装,增强改变

Java IO中的装饰模式

  在IO中,具体构件角色是节点流,装饰角色是过滤流

  FilterInputStream和FilterOutputStream是装饰角色,而其他派生自它们的类则是具体装饰角色。

我们来看看代码

抽象角色类InputStream

 public abstract class InputStream implements Closeable {

 //实现了read等方法
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
} }

具体角色类FileInputStream

 public
class FileInputStream extends InputStream{ }

抽象包装类FilterInputStream

 public
class FilterInputStream extends InputStream {
/**
* The input stream to be filtered.
*/
protected volatile InputStream in; protected FilterInputStream(InputStream in) {
this.in = in;
} public int read() throws IOException {
return in.read();
} }

具体包装类BufferedInputStream

 public
class BufferedInputStream extends FilterInputStream { public BufferedInputStream(InputStream in) {
this(in, defaultBufferSize);
} //完成了设置抽象角色类的映射
public BufferedInputStream(InputStream in, int size) {
super(in);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
} //read方法中调用fill()
public synchronized int read() throws IOException {
if (pos >= count) {
fill();
if (pos >= count)
return -1;
}
return getBufIfOpen()[pos++] & 0xff;
}
private void fill() throws IOException {
byte[] buffer = getBufIfOpen();
if (markpos < 0)
pos = 0; /* no mark: throw away the buffer */
else if (pos >= buffer.length) /* no room left in buffer */
if (markpos > 0) { /* can throw away early part of the buffer */
int sz = pos - markpos;
System.arraycopy(buffer, markpos, buffer, 0, sz);
pos = sz;
markpos = 0;
} else if (buffer.length >= marklimit) {
markpos = -1; /* buffer got too big, invalidate mark */
pos = 0; /* drop buffer contents */
} else { /* grow buffer */
int nsz = pos * 2;
if (nsz > marklimit)
nsz = marklimit;
byte nbuf[] = new byte[nsz];
System.arraycopy(buffer, 0, nbuf, 0, pos);
if (!bufUpdater.compareAndSet(this, buffer, nbuf)) {
// Can't replace buf if there was an async close.
// Note: This would need to be changed if fill()
// is ever made accessible to multiple threads.
// But for now, the only way CAS can fail is via close.
// assert buf == null;
throw new IOException("Stream closed");
}
buffer = nbuf;
}
count = pos;
int n = getInIfOpen().read(buffer, pos, buffer.length - pos);
if (n > 0)
count = n + pos;
} }

fill()方法中除了自身操作外,我们看到了 int n = getInIfOpen().read(buffer, pos, buffer.length - pos);

  private InputStream getInIfOpen() throws IOException {
InputStream input = in;
if (input == null)
throw new IOException("Stream closed");
return input;
}

在看看getInIfOpen()方法,我们就明白了 整个的过程

参考地址:

http://www.cnblogs.com/mengdd/archive/2013/02/12/2910302.html

http://blog.csdn.net/cai1213/article/details/8003445

http://xubindehao.iteye.com/blog/474636

《JAVA与模式》之装修者模式的更多相关文章

  1. JAVA设计模式之【装饰者模式】

    JAVA设计模式之[装饰者模式] 装饰模式 对新房进行装修并没有改变房屋的本质,但它可以让房子变得更漂亮.更温馨.更实用. 在软件设计中,对已有对象(新房)的功能进行扩展(装修). 把通用功能封装在装 ...

  2. 重学 Java 设计模式:实战工厂方法模式

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获!

  3. 重学 Java 设计模式:实战抽象工厂模式

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获!

  4. 重学 Java 设计模式:实战装饰器模式(SSO单点登录功能扩展,增加拦截用户访问方法范围场景)

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 对于代码你有编程感觉吗 很多人写代码往往是没有编程感觉的,也就是除了可以把功能按照固 ...

  5. 重学 Java 设计模式:实战享元模式「基于Redis秒杀,提供活动与库存信息查询场景」

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 程序员‍‍的上下文是什么? 很多时候一大部分编程开发的人员都只是关注于功能的实现,只 ...

  6. Java 的设计模式之一装饰者模式

    刚开始接触装饰者的设计模式,感觉挺难理解的,不够后来花了一个晚上的时间,终于有头绪了 装饰者设计模式:如果想对已经存在的对象进行装饰,那么就定义一个类,在类中对已经有的对象进行功能的增强或添加另外的行 ...

  7. Java设计模式12:装饰器模式

    装饰器模式 装饰器模式又称为包装(Wrapper)模式.装饰器模式以多客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰器模式的结构 通常给对象添加功能,要么直接修改对象添加相应的功能, ...

  8. Java设计模式3:工厂方法模式

    工厂方法模式 工厂方法模式是类的创建模式.工厂方法模式的用意是定义一个创建产品对象的工厂接口,将实际创建工厂推迟到子类中. 工厂方法模式 工厂方法模式是对简单工厂模式进一步抽象的结果. 假如是不使用反 ...

  9. Java设计模式2:简单工厂模式

    简单工厂模式 简单工厂模式是类的创建模式,又叫做静态工厂方法模式.简单工厂模式由一个工厂对象决定生产出哪一种产品类的实例. 为什么要使用简单工厂模式 原因很简单:解耦. A对象如果要调用B对象,最简单 ...

随机推荐

  1. .NET 开发套装

    Dapper,轻量级ORM GitHub - StackExchange/dapper-dot-net: DapperSharpZipLib,ZIP压缩库 SharpZipLib by icsharp ...

  2. Android Jackson 概述

    原文地址 本文内容 JSON 的三种方式 示例 完全数据绑定(POJO)示例 "Raw"数据绑定示例 用泛型数据绑定 树模型(Tree Model)示例 流(Streaming)A ...

  3. springmvc-servlet.xml中use-default-filters的作用

    1.<!-- 启用注解扫描,并定义组件查找规则 ,mvc层只负责扫描@Controller --> [java] view plain copy <context:component ...

  4. 解决 IllegalArgumentException: Could not resolve placeholder in string value "${XXXXXX}"

    如题: 导致这一问题的原因:使用了重复的property-placeholder 如一个配置文件中使用了 <context:property-placeholder location=" ...

  5. 微软BI 之SSAS 系列 - 基于雪花模型的维度设计

    基于雪花模型的维度以下面的 Product 产品与产品子类别,产品类别为例. DimProduct 表和 DimProductSubcategory 表有外键关系,而 DimProductSubcat ...

  6. ububuntu配置ip和dns

    装完ubuntu 第一件事情就是连上网,换个源,进行更新操作,但前提条件是要配好ip和dns. 下面把自己配置的过程记录下来,权且当作一份备份,以便不时之需. 一.配置ip ubuntu的网络配置信息 ...

  7. linux 新进程的创建

    慕课18原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.背景知识: 1. ...

  8. ios app性能分析

    苹果app的流畅性一般比安卓的要好的多.应该是和苹果系统的设计理念同样,早期的iphone4曾经是绝对单任务,仅仅能做一件事情,尽管添加了后台能够.音乐播放,定位等有限的服务.可是大多数普通应用切换到 ...

  9. python获取PING结果

    # -*- coding: utf-8 -*- import subprocess import re def get_ping_result(ip_address): p = subprocess. ...

  10. VB 求余求整

    可以直接用函数来实现: 1.用CInt()函数的范围在-32,768 至 32,767,对于小数部分四舍五入 . 2.用Int()函数和Fix()函数都会删除参数的小数部份而返回剩下的整数, 不同之处 ...