例如现在有这样一个需求:读取一个文件里面的内容,将文件里的内容所有的小写字母都转换成大写的。这个时候我们就可以通过去扩展IO流去做。
package com.unis.io;
2.
3.import java.io.BufferedReader;
4.import java.io.File;
5.import java.io.FileReader;
6.import java.io.FilterReader;
7.import java.io.IOException;
8.import java.io.Reader;
9.
10.public class UppCaseReader extends FilterReader {
11.
12. public UppCaseReader(Reader in) {
13. super(in);
14. }
15.
16. @Override
17. public int read(char[] cbuf, int off, int len) throws IOException {
18. int result = super.read(cbuf, off, len);
19. for(int i=off;i<len;i++){
20. if(cbuf[i]>='a'&&cbuf[i]<='z'){
21. cbuf[i] -= 32;
22. }
23. }
24. return result;
25. }
26.
27.// @Override
28.// public int read() throws IOException {
29.// int result = super.read();
30.// if(result>='a'&&result<='z'){
31.// result-=32;
32.// }
33.// return result;
34.// }
35.
36. public static void main(String[] args) throws IOException {
37. Reader reader = new BufferedReader(new UppCaseReader(new FileReader(new File("src/com/unis/io/UppCaseReader.java"))));
38. int i =0;
39. while((i=reader.read())!=-1){
40. System.out.print((char)i);
41. }
42. }
43.}
Reader reader = new BufferedReader(new UppCaseReader(new FileReader(new File("src/com/unis/io/UppCaseReader.java"))));
是典型的装饰器模式的运用语句。每封装一次,就添加一个新的功能。
Java装饰模式
装饰模式:给一个类添加一些额外的职责,并且在添加这些额外的职责时不会控制该类的执行逻辑。 装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。
装饰模式以对客户端透明的方式动态的给一个对象附加上更多的责任。换言之客户端并不会觉的对象在装饰前和装饰后有什么区别。
实例化 装饰者子类的时候,是调用构建者子类来实例化,不会调用装饰者类,装饰者类对客户端来说是透明的,只是对功能的扩张。
UML类图:
装饰模式与类继承的区别
1: 装饰模式是一种动态行为,对已经存在类进行随意组合,而类的继承是一种静态的行为,一个类定义成什么样的,该类的对象便具有什么样的功能,无法动态的改变。
2: 装饰模式扩展的是对象的功能,不需要增加类的数量,而类继承扩展是类的功能,在继承的关系中,如果我们想增加一个对象的功能,我们只能通过继承关系,在子类中增加两个方法。
3: 装饰模式是在不改变原类文件和使用继承的情况下,动态的扩展一个对象的功能,它是通过创建一个包装对象,也就是装饰来包裹真是的对象。
组成部分:
抽象构件:原始的功能接口-----相当于i/o流里面InputStream/OutputStream和Reader/Writer。
具体构件:具体的原始功能类----实现接口的类---------------相当于i/o里面的FileOutputStream和FileInputStream。
装饰角色:持有具体构件类的对象,以便执行原有功能------接口的实现类,类里调用接口的对象 --------------相当于i/o里面的FilterOutputStream和FilterInputStream。
具体装饰:具体扩展的功能在这里--------------------------------相当于i/o流里面的BufferedOutputStream和BufferedInputStream以及DataOutputStream和DataInputSrtream。
- public interface IComponent {
- void Operation();
- }
- public class Component implements IComponent{
- public void Operation()
- {
- System.out.println("Component Operation");
- }
- }
- public abstract class Decorator implements IComponent {
- protected IComponent realComponnet;
- public Decorator(IComponent component) {
- realComponnet = component;
- }
- @Override
- public void Operation() {
- // TODO Auto-generated method stub
- if (realComponnet != null) {
- realComponnet.Operation();
- }
- }
- }
- public class AuthDecorator extends Decorator {
- /**
- * @param component
- */
- public AuthDecorator(IComponent component) {
- super(component);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void Operation() {
- // TODO Auto-generated method stub
- System.out.println("Befor Operation Authorization");
- super.Operation();
- System.out.println("After Operation Authorization");
- }
- }
- public class LogDecorator extends Decorator {
- /**
- * @param component
- */
- public LogDecorator(IComponent component) {
- super(component);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void Operation() {
- // TODO Auto-generated method stub
- System.out.println("Operation Logging");
- super.Operation();
- System.out.println("Operation Log Finished");
- }
- }
在Java IO中
DataoutputStream out=new DataoutputStream(new FileoutputStream());
这就是 装饰者模式,DataoutputStream是装饰者子类,FileoutputStream是实现接口的子类。
这里不会调用到装饰者类--FilteroutputStream,只是作为继承的另一种方案,对客户端来说是透明的,是为了功能的扩张.
这就是为什么平时学java时,不会接触到FilteroutputStream这个类的原因
输出结果:
晚上
开车
其实装饰者就是给对象穿马甲 多穿一层就多加一点功能
转载于:http://www.cnblogs.com/ChrisRIM/archive/2012/08/21/2648372.html
public BufferedInputStream ( InputStream in) { this(in, defaultBufferSize );
转载于:http://mengzhenbin.iteye.com/blog/1395613