目录
一、关于装饰模式
设计模式:6、装饰模式(包装器)_装饰者模式uml描述-优快云博客
二、设计要求
本设计要求的核心是扩展已有的一个系统以满足用户需求。
当前系统已有一个抽象类ReadWord,该类有一个抽象方法readWord(),另外,系统还有一个ReadWord类的子类ReadEnglishWord,该类的readWord()方法可以读取一个由英文单词构成的文本文件word.txt。
目前已有一些客户在使用该系统,并使用ReadWord类的对象调用readWord()方法读取文件中的单词。
英文单词构成的文件word.txt的格式是每行只有一给单词,内容如下(文件建在包所在目录下):
arrange
example
intellgence
ReadWord和ReadEnglishWord类代码如下:
package xyz.jangle.design.app.decorator;
import java.io.File;
import java.util.ArrayList;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2026年10月11日 下午2:26:28
*
*/
public abstract class ReadWord {
public abstract ArrayList<String> readWord(File file);
}
package xyz.jangle.design.app.decorator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2026年10月11日 下午2:28:06
*
*/
public class ReadEnglishWord extends ReadWord {
@Override
public ArrayList<String> readWord(File file) {
ArrayList<String> arrayList = new ArrayList<String>();
try(FileReader inOne = new FileReader(file);
BufferedReader inTwo = new BufferedReader(inOne)){
String s = null;
while((s = inTwo.readLine()) != null) {
arrayList.add(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return arrayList;
}
}
现在有部分用户希望使用ReadWord类的对象调用readWord()方法读取文件word.txt中的单词,并希望同时也能得到该单词的汉语解释,也有一些用户希望不仅能得到该单词的汉语解释也能得到该单词的英文例句,而其他用户没有提出任何要求。要求不允许修改现有系统的代码以及word.txt文件,对系统进行拓展以满足用户需求。
三、设计实现
由于不允许修改原有系统中的代码和文件,因此决定使用装饰模式来拓展原系统,设计的类图如下所示:

1.抽象组件
ReadWord类
2.具体组件
ReadEnglishWord类
3.装饰
在原系统中添加一个装饰,该装饰是Decorator类,代码如下:
package xyz.jangle.design.app.decorator;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2026年10月11日 下午2:34:15
*
*/
public abstract class Decorator extends ReadWord {
protected ReadWord reader;
public Decorator(ReadWord reader) {
super();
this.reader = reader;
}
}
4.具体装饰
具体装饰是WordDecorator类,该类使用另外一个文件来装饰“被装饰者”读取的word.txt文件
WordDecorator类代码如下:
package xyz.jangle.design.app.decorator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2026年10月11日 下午2:36:01
*
*/
public class WordDecorator extends Decorator {
File decoratorFile;
public WordDecorator(ReadWord reader, File decoratorFile) {
super(reader);
this.decoratorFile = decoratorFile;
}
@Override
public ArrayList<String> readWord(File file) {
ArrayList<String> wordList = reader.readWord(file);
try(FileReader inOne = new FileReader(decoratorFile);
BufferedReader inTwo = new BufferedReader(inOne)){
String s = null;
int m = 0;
while((s = inTwo.readLine())!=null) {
String word = wordList.get(m);
wordList.set(m, word +"|"+s);
m++;
if(m>wordList.size())break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wordList;
}
}
5.相关文件
另外需要两个用于装饰的文本文件,分别是chiness.txt和englishSentence.txt。他们每行的文本内容分别是原系统word.txt中对应行上单词的汉语解释和英文例句,内容如下(文件建在包所在目录下):
chinese.txt
整理
例子
智力
englishSentence.txt
arrange tools in order
cite an example in point
an intelligence test
四、应用程序
package xyz.jangle.design.app.decorator;
import java.io.File;
import java.util.ArrayList;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2026年10月11日 下午2:48:41
*
*/
public class Application {
public static void main(String[] args) {
// 读取英文
ReadEnglishWord readEnglishWord = new ReadEnglishWord();
String filePath = "src/xyz/jangle/design/app/decorator";
ArrayList<String> readWord = readEnglishWord.readWord(new File(filePath+"/word.txt"));
for(String s:readWord) {
System.out.println(s);
}
// 读取英文和中文
WordDecorator readChinessWord = new WordDecorator(readEnglishWord,new File(filePath+"/chiness.txt"));
ArrayList<String> chinessWord = readChinessWord.readWord(new File(filePath+"/word.txt"));
for(String s:chinessWord) {
System.out.println(s);
}
// 读取英文,中文和例句
WordDecorator readSentenceWord = new WordDecorator(readChinessWord,new File(filePath+"/englishSentence.txt"));
ArrayList<String> sentenceWord = readSentenceWord.readWord(new File(filePath+"/word.txt"));
for(String s:sentenceWord) {
System.out.println(s);
}
}
}
五、执行结果

308



被折叠的 条评论
为什么被折叠?



