装饰模式(别名:包装模式):
动态的给对象添加一些额外的职责。就功能来说装饰模式比生成子类更为灵活。
Decorator Pattern(Another Name: Wrapper Pattern):
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
装饰者与被装饰者实现同一个接口,在装饰者中添加一个所实现的接口类型的成员变量,并且在构造方法中初始化,在具体方法中调用该成员变量的相同方法,除此之外添加所需要添加的内容。
下面举一个例子:
import java.io.File;
import java.util.ArrayList;
public class Application {
/**
* 题目要求:
* 利用包装模式扩展现有系统以满足客户要求
* 当前系统有一个抽象类ReadWord,该类有一个抽象方法readWord(),另外,系统还有一个ReadWord类的子类ReadEnglishWord,
* 该类的readWord()方法可以读取一个由英文单词构成的文本文件word.txt
*
* 现有部分用户希望使用ReadWord类的对象调用readWord方法读取文件word.txt中的单词,并同时也希望得到该单词的汉语解释,
* 也有一些用户希望不仅能得到该单词的汉语解释也能的到该单词的英语例句,而其他用户没有提出任何要求。
*
* 要求不允许修改现有系统的代码以及word.txt文件,对系统进行扩展以满足用户的需求
*/
public static void main(String[] args) {
ReadEnglishWord readEnglishWord = new ReadEnglishWord();
WordDecorator wordDecorator1 = new WordDecorator(readEnglishWord, new File( "D:\\decoratorTest\\chinese.txt"));
ArrayList<String> wordList = wordDecorator1.readWrod(new File( "D:\\decoratorTest\\word.txt"));
for (String s : wordList) {
System.out.println(s);
}
WordDecorator wordDecorator2 = new WordDecorator(wordDecorator1, new File( "D:\\decoratorTest\\englishSentence.txt"));
wordList = wordDecorator2.readWrod(new File( "D:\\decoratorTest\\word.txt"));
for (String s : wordList) {
System.out.println(s);
}
}
}
import java.io.File;
import java.util.ArrayList;
public interface ReadWord {
public ArrayList<String> readWrod(File file);
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class ReadEnglishWord implements ReadWord {
@Override
public ArrayList<String> readWrod(File file) {
ArrayList<String> wordList = new ArrayList<>();
try {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String str = null;
while ((str = bufferedReader.readLine()) != null){
wordList.add(str);
}
bufferedReader.close();
fileReader.close();
}catch (IOException e){
e.printStackTrace();
}
return wordList;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class WordDecorator implements ReadWord {
private ReadWord readWord;
private File docoratorFile;
public WordDecorator(ReadWord readWord, File docoratorFile) {
this.readWord = readWord;
this.docoratorFile = docoratorFile;
}
@Override
public ArrayList<String> readWrod(File file) {
ArrayList<String> wordList = readWord.readWrod(file);
try{
FileReader fileReader = new FileReader(docoratorFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String str = null;
int index = 0;
while ((str = bufferedReader.readLine()) != null){
String word = wordList.get(index);
word = word.concat(" | " + str);
wordList.set(index, word);
index++;
if (index > wordList.size()){
break;
}
}
fileReader.close();
bufferedReader.close();
}catch (IOException e){
e.printStackTrace();
}
return wordList;
}
}
word.txt chinese.txt englishSentence.txt文件中的内容分别为:
arrange
example
intelligence
安排
例子
智力
arrange tools in order
cite an example in point
an intelligence test
程序的运行输出结果为:
arrange | 安排
example | 例子
intelligence | 智力
arrange | 安排 | arrange tools in order
example | 例子 | cite an example in point
intelligence | 智力 | an intelligence test
拓展:装饰者的类中一般没有无参的构造方法,因为是要对原有对象进行包装的,如果无参也就失去了包装的意义。
java.io包中的类就是装饰模式的典型代表。