常用设计模式----装饰者模式

package org.design.patterns;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

/**
 * 装饰者模式:
 * 动态地将责任附加到对象上。若要扩展功能,装饰者提供了比  "继承" 更有弹性的替代方案。 
 *
 * 设计原则:
 * 类应该对扩展开放,对修改关闭。
 *
 *特点:
 *--装饰者和被装饰对象有相同的超类型(如下文中的InputStream);
 *--可以用一个或多个装饰者包装一个对象;
 *--装饰者可以在所委托被装饰者的行为前/后,加上自己的行为,已达到特定的目的;
 *
 *==========================================
 *Java I/O中的有装饰者模式的体现
 *FilterInputStream及FilterOutputStream实现了装饰器模式
 *例如: new LineNumberInputStream(new BufferedInputStream(new FileInputStream(new File(""))))
 *其中LineNumberInputStream(加上了计算行数的能力)和BufferedInputStream(加入缓冲器)
 *都为具体装饰者,都是FilterInputStream的子类。
 *
 *Java I/O引入装饰者模式有一个"缺点":利用装饰者模式,常常照成设计中有大量的小类,数量多了就容易造成
 *困扰,理解装饰者模式的原理,有助于辨别装饰者类的组织结构。
 *
 *当然亦可扩展FilterInputStream编写自己特定目的的装饰者类(重写其read方法)。
 */

//FilterInputStream所有"装饰者"的基类,下面是其实现
class FilterInputStream extends InputStream {
 protected volatile InputStream in;//"被装饰"的对象的基类型

 protected FilterInputStream(InputStream in) {
  this.in = in;
 }
 
 public int read() throws IOException {
  return in.read();
 }
 public int read(byte b[]) throws IOException {
  return read(b, 0, b.length);
 }
 public int read(byte b[], int off, int len) throws IOException {
  return in.read(b, off, len);
 }

 public long skip(long n) throws IOException {
  return in.skip(n);
 }
 public int available() throws IOException {
  return in.available();
 }
 public void close() throws IOException {
  in.close();
 }
 public synchronized void mark(int readlimit) {
  in.mark(readlimit);
 }
 public synchronized void reset() throws IOException {
  in.reset();
 }
 public boolean markSupported() {
  return in.markSupported();
 }
}


//==================================================
/*Java I/O中的适配器模式的体现
 *两个适配器类:InputStreamReader和OutputStreamWriter实现了字节流到字符流的转换
 *例如:new BufferedReader(new InputStreamReader(new FileInputStream(new File(""))));
 */
//InputStreamReader实现了接口的转换
class InputStreamReader extends Reader {
 private final StreamDecoder sd;

 public InputStreamReader(InputStream in) {
  super(in);
  try {
   sd = StreamDecoder.forInputStreamReader(in, this, (String) null);
  } catch (UnsupportedEncodingException e) {
   throw new Error(e);
  }
 }

 public InputStreamReader(InputStream in, String charsetName)
   throws UnsupportedEncodingException {
  super(in);
  if (charsetName == null)
   throw new NullPointerException("charsetName");
  sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
 }

 public InputStreamReader(InputStream in, Charset cs) {
  super(in);
  if (cs == null)
   throw new NullPointerException("charset");
  sd = StreamDecoder.forInputStreamReader(in, this, cs);
 }

 public InputStreamReader(InputStream in, CharsetDecoder dec) {
  super(in);
  if (dec == null)
   throw new NullPointerException("charset decoder");
  sd = StreamDecoder.forInputStreamReader(in, this, dec);
 }

 public String getEncoding() {
  return sd.getEncoding();
 }

 public int read() throws IOException {
  return sd.read();
 }
 public int read(char cbuf[], int offset, int length) throws IOException {
  return sd.read(cbuf, offset, length);
 }
 public boolean ready() throws IOException {
  return sd.ready();
 }

 public void close() throws IOException {
  sd.close();
 }
}

你可能感兴趣的:(装饰者模式)