Adapter pattern
In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface.[1] It is often used to make existing classes work with others without modifying their source code.
An example is an adapter that converts the interface of a Document Object Model of an XML document into a tree structure that can be displayed.
Structure

Example
public class Main {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("c:/test.text");
//Adapter
InputStreamReader isr = new InputStreamReader(fis);
//public BufferedReader(Reader in)
//BufferedReader 的参数为Reader
//InputstreamReader 将 FileInputStream fis 转为 Reader
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();
while (line != null && !line.equals("")) {
System.out.println(line);
}
br.close();
}
}
Summary
- 类似于转接头的中转作用
- 常见的Adapter类反而不是Adapter
- WindowAdapter
- KeyAdapter

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



