1.装饰模式---对装饰与内容一视同仁
2.java中的 FileInputStream BufferedInputStream DataInputStream 是典型的装饰模式的应用。
比如下面的代码:
FileOutputStream fos = new FileOutputStream("1.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
byte b = 3;
int i = 78;
char ch = 'a';
float f = 2.3f;
double d = 2.445534;
dos.writeByte(b);
dos.writeInt(i);
dos.writeChar(ch);
dos.writeFloat(f);
dos.writeDouble(d);
dos.close();
FileInputStream fis = new FileInputStream("1.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
System.out.println(dis.readByte());
System.out.println(dis.readInt());
System.out.println(dis.readChar());
System.out.println(dis.readFloat());
System.out.println(dis.readDouble());
dis.close();
装饰的含义就是在最开始的个体上不断地添加新的装饰物体,最后成为我们想要的东西。
2.示例:
package com.cn;
abstract class Display {
public abstract int getColumns();
public abstract int getRows();
public abstract String getRowText(int row);
public final void show() {
for (int i = 0; i < getRows(); i++) {
System.out.println(getRowText(i));
}
}
}
class StringDisplay extends Display {
private String string;
public StringDisplay(String string) {
this.string = string;
}
public int getColumns() {
return string.getBytes().length;
}
public int getRows() {
return 1;
}
public String getRowText(int row) {
if (row == 0) {
return string;
}
else {
return null;
}
}
}
abstract class Border extends Display {
protected Display display;
protected Border(Display display) {
this.display = display;
}
}
class SideBorder extends Border {
private char borderChar;
public SideBorder(Display display, char ch) {
super(display);
this.borderChar = ch;
}
public int getColumns() {
return 1 + display.getColumns() + 1;
}
public int getRows() {
return display.getRows();
}
public String getRowText(int row) {
return borderChar + display.getRowText(row) + borderChar;
}
}
class FullBorder extends Border {
public FullBorder(Display display) {
super(display);
}
public int getColumns() {
return 1 + display.getColumns() + 1;
}
public int getRows() {
return 1 + display.getRows() + 1;
}
public String getRowText(int row) {
if(row == 0) {
return "+" + makeLine('-',display.getColumns()) + "+";
}
else if (row == display.getRows() + 1) {
return "+" + makeLine('-',display.getColumns()) + "+";
}
else {
return "|" + display.getRowText(row - 1) + "|";
}
}
private String makeLine(char ch, int count) {
StringBuffer buf = new StringBuffer();
for(int i = 0; i < count; i++) {
buf.append(ch);
}
return buf.toString();
}
}
public class Decorator {
public static void main(String[] args) {
/*Display b1 = new StringDisplay("Hello,world.");
Display b2 = new SideBorder(b1,'#');
Display b3 = new FullBorder(b2);
b1.show();
b2.show();
b3.show();*/
Display b4 = new SideBorder(new FullBorder(new FullBorder(new SideBorder(new FullBorder(new StringDisplay("你好。")),'*'))),'/');
b4.show();
}
}
3.另外一个示例(展现此模式的魅力)
package com.cn;
abstract class Display2 {
public abstract int getColumns();
public abstract int getRows();
public abstract String getRowText(int row);
public void show() {
for(int i = 0; i < getRows(); i++) {
System.out.println(getRowText(i));
}
}
}
class StringDisplay2 extends Display2 {
private String string;
public StringDisplay2(String string) {
this.string = string;
}
public int getColumns() {
return string.getBytes().length;
}
public int getRows() {
return 1;
}
public String getRowText(int row) {
if(row == 0) {
return string;
}
else {
return null;
}
}
}
abstract class Boder extends Display2 {
protected Display2 display;
protected Boder(Display2 display) {
this.display = display;
}
}
class FullBoder extends Boder {
char ch;
public FullBoder(Display2 display, char ch) {
super(display);
this.ch = ch;
}
public int getColumns() {
return 1 + display.getColumns() + 1;
}
public int getRows() {
return 1 + display.getRows() + 1;
}
public String getRowText(int row) {
if(row == 0 || row == getRows()-1) {
return print();
}
else {
return ch + display.getRowText(row - 1) + ch;
}
}
private String print() {
StringBuffer sb = new StringBuffer();
for(int i = 0; i < getColumns(); i++) {
sb.append(ch);
}
return sb.toString();
}
}
public class Main {
public static void main(String[] args) {
StringDisplay2 sd = new StringDisplay2("+");
FullBoder fb = new FullBoder(sd,'-');
FullBoder fb2 = new FullBoder(fb,'+');
fb2.show();
}
}
装饰模式有一点嵌套循环的意思。