装饰者模式(Decorator Pattern) Java的IO类 用法

本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716823

装饰者模式(decorator pattern)參见: http://blog.csdn.net/caroline_wendy/article/details/26707033

Java的IO类使用装饰者模式进行扩展, 当中FilterInputStream类, 就是装饰者(decorator)的基类.

实现其它装饰者(decorator), 须要继承FilterInputStream类.

代码:

/**
* @time 2014年5月23日
*/
package decorator.io; import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream; /**
* @author C.L.Wang
*
*/
public class LowerCaseInputStream extends FilterInputStream { public LowerCaseInputStream(InputStream in) {
super(in);
} public int read() throws IOException {
int c = super.read();
return (c==-1 ? c : Character.toLowerCase((char)c));
} public int read(byte[] b, int offset, int len) throws IOException {
int result = super.read(b, offset, len);
for (int i=offset; i<offset+result; ++i) {
b[i] = (byte)Character.toLowerCase((char)b[i]);
}
return result;
}
}

測试:

/**
* @time 2014年5月23日
*/
package decorator.io; import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; /**
* @author C.L.Wang
*
*/
public class InputTest { /**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
int c;
try{
InputStream in = new LowerCaseInputStream(new BufferedInputStream(new FileInputStream("test.txt")));
while ((c = in.read()) >= 0) {
System.out.print((char)c);
}
in.close();
} catch (IOException e){
e.printStackTrace();
}
} }

通过装饰详细组件类FileInputStream, 实现格式的更改.

注意:Java的文件的默认读取路径为项目的根文件夹.

设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法的更多相关文章

  1. 浅谈设计模式--装饰者模式(Decorator Pattern)

    挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...

  2. 设计模式 - 装饰者模式(Decorator Pattern) 具体解释

    装饰者模式(Decorator Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033 装饰者 ...

  3. C#设计模式——装饰者模式(Decorator Pattern)

    一.例子在软件开发中,我们往往会想要给某一类对象增加不同的功能.比如要给汽车增加ESP.天窗或者定速巡航.如果利用继承来实现,就需要定义无数的类,Car,ESPCar,CCSCar,SunRoofCa ...

  4. 设计模式学习--装饰者模式(Decorator Pattern)

    概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...

  5. 大话设计模式--装饰者模式 Decorator -- C++实现实例

    1.装饰者模式 Decorator 动态地给一个对象添加一个额外的职责, 就添加功能来说, 装饰模式比生成子类更为灵活. 每个装饰对象的实现和如何使用这个对象分离,  每个装饰对象只关心自己的功能,不 ...

  6. 23种设计模式之装饰器模式(Decorator Pattern)

    装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...

  7. 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)

    在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...

  8. C#设计模式之装饰者模式(Decorator Pattern)

    1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...

  9. Android设计模式之中的一个个样例让你彻底明确装饰者模式(Decorator Pattern)

    导读 这篇文章中我不会使用概念性文字来说明装饰者模式.由于通常概念性的问题都非常抽象.非常难懂.使得读者非常难明确究竟为什么要使用这样的设计模式.我们设计模式的诞生,肯定是前辈们在设计程序的时候遇到了 ...

随机推荐

  1. 运行计划之误区,为什么COST非常小,SQL却跑得非常慢?

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/38321477 2014.7.31就晚20:30 My Oracle Support组猫大师 ...

  2. [LeetCode] Decode Ways [33]

    题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...

  3. ACM第三次比赛 Big Chocolate

    Problem G Big Chocolate Mohammad has recently visited Switzerland . As he loves his friends very muc ...

  4. 如何创建C++程序

    下载Microsoft Visual C++ 6.0请点击这里:VC 6.0下载(包括中文版英文版)(支持Win7和XP) 首先,我们要进入Microsoft Visual C++集成开发环境(Int ...

  5. HDOJ 4007 Dave【最大覆盖集】

    Dave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submis ...

  6. javascript笔记整理(正则)

    RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具 var re=/e/; var re=new RegExp('e'); 正则表达式的 String 对象的方法 1.search- ...

  7. Codeforces Round #189 (Div. 2)

    题目地址:http://codeforces.com/contest/320 第一题:基本题,判断mod 1000,mod 100.,mod 10是不是等于144.14.1,直到为0 代码如下: #i ...

  8. 它们的定义View

    Ios"巷自己的定义View和Android类别似 在.h文件设置了他的一些财产.方法 在.m文件中实现 .h文件 #import <UIKit/UIKit.h> CGPoint ...

  9. linux命令: mount

    mount使用示例: #mkdir -p /mnt/usbhd1 注:建立目录用来作挂接点(mount point) #mount -t ntfs /dev/sdc1 /mnt/usbhd1 (-t ...

  10. net core 中间件详解及项目实战

    net core 中间件详解及项目实战 前言 在上篇文章主要介绍了DotNetCore项目状况,本篇文章是我们在开发自己的项目中实际使用的,比较贴合实际应用,算是对中间件的一个深入使用了,不是简单的H ...