Java IO<4>Java io與裝飾器模式
Java io與裝飾器模式
裝飾器模式
裝飾器模式(Decorator Pattern)允許向一個現有的物件新增新的功能,同時又不改變其結構。這種型別的設計模式屬於結構型模式,它是作為現有的類的一個包裝。
定義:裝飾模式是在不必改變原類檔案和使用繼承的情況下,動態的擴充套件一個物件的功能。它是通過建立一個包裝物件,也就是裝飾來包裹真實的物件。
● Component抽象構件
Component是一個介面或者是抽象類,就是定義我們最核心的物件,也就是最原始的物件。
注意在裝飾模式中,必然有一個最基本、最核心、最原始的介面或抽象類充當Component抽象構件。
● ConcreteComponent 具體構件
ConcreteComponent是最核心、最原始、最基本的介面或抽象類的實現,你要裝飾的就是它。
● Decorator裝飾角色
一般是一個抽象類,做什麼用呢?實現介面或者抽象方法,它裡面可不一定有抽象的方法,在它的屬性裡必然有一個private變數指向Component抽象構件
● 具體裝飾角色
ConcreteDecoratorA和ConcreteDecoratorB是兩個具體的裝飾類,你要把你最核心的、最原始的、最基本的東西裝飾成其他東西。
裝飾器舉例
public interface Component { void method(); } public class ConcreteComponent implements Component{ public void method() { System.out.println("原來的方法"); } } public abstract class Decorator implements Component{ protected Component component; public Decorator(Component component) { super(); this.component = component; } public void method() { component.method(); } } public class ConcreteDecoratorA extends Decorator{ public ConcreteDecoratorA(Component component) { super(component); } public void methodA(){ System.out.println("被裝飾器A擴充套件的功能"); } public void method(){ System.out.println("針對該方法加一層A包裝"); super.method(); System.out.println("A包裝結束"); } }
以BufferedReader為例,裡面也包含一個Reader物件,BufferedReader對Reader進行了裝飾
Java io中的裝飾器模式
根據上圖可以看出:
●抽象構件(Component)角色:由OutputStream扮演。這是一個抽象類,為各種子型別提供統一的介面。
●具體構件(ConcreteComponent)角色:由ByteArrayOutputStream、FileOutputStream、ObjectOutputStream、PipedOutputStream等類扮演。它們實現了抽象構件角色所規定的介面。
●抽象裝飾(Decorator)角色:由FilterOutputStream扮演。它實現了OutputStream所規定的介面。
●具體裝飾(ConcreteDecorator)角色:由幾個類扮演,分別是BufferedOutputStream、DataOutputStream等類扮演。
OutputStream原始碼:
//OutputStream
package java.io;
public abstract class OutputStream implements Closeable, Flushable {
public abstract void write(int b) throws IOException;
public void write(byte b[]) throws IOException {}
public void write(byte b[], int off, int len) throws IOException {}
public void flush() throws IOException { }
public void close() throws IOException { }
}
具體的裝飾器FilterOutputStream:作為裝飾模式的抽象裝飾角色FilterOutputStream類的原始碼。
package java.io;
public class FilterOutputStream extends OutputStream {
protected OutputStream out;
public FilterOutputStream(OutputStream out) {
this.out = out;
}
public void write(int b) throws IOException {
out.write(b);
}
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
public void write(byte b[], int off, int len) throws IOException {
if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
throw new IndexOutOfBoundsException();
for (int i = 0; i < len; i++) {
write(b[off + i]);
}
}
public void flush() throws IOException {
out.flush();
}
@SuppressWarnings("try")
public void close() throws IOException {
try (OutputStream ostream = out) {
flush();
}
}
}
具體的裝飾角色BufferedOutputStream:
package java.io;
public class BufferedOutputStream extends FilterOutputStream {
protected byte buf[];
protected int count;
public BufferedOutputStream(OutputStream out) {}
public BufferedOutputStream(OutputStream out, int size) {}
private void flushBuffer() throws IOException {}
public synchronized void write(int b) throws IOException {}
public synchronized void write(byte b[], int off, int len) throws IOException {}
public synchronized void flush() throws IOException {}
}
●抽象構件(Component)角色:由Writer扮演。這是一個抽象類,為各種子型別提供統一的介面。
●具體構件(ConcreteComponent)角色:由BufferedWriter、CharArrayWriter、FilterWriter、OutputStreamWriter、PipedWriter、PrintWriter、StringWriter扮演。它們實現了抽象構件角色所規定的介面。
●抽象裝飾(Decorator)角色:由OutputStreamWriter扮演。它實現了Writer所規定的介面。
●具體裝飾(ConcreteDecorator)角色:由FileWriter扮演。