Adapter
Adapter模式也被称为Wrapper模式,分为
- 类适配器模式(使用继承的适配器)
/**
* 需求接口,定义所需的方法
*/
public interface Print {
public abstract void printWeak();
public abstract void printStrong();
}
/**
* 实际情况//被适配
*/
public class Banner {
private String string;
public Banner(String string) {
this.string = string;
}
public void showWithParen(){
System.out.println("("+string+")");
}
public void showWithAster(){
System.out.println("*"+string+"*");
}
}
/**
* 适配器
*/
public class PrintBanner extends Banner implements Print {
public PrintBanner(String string) {
super(string);
}
@Override
public void printWeak() {
showWithParen();
}
@Override
public void printStrong() {
showWithAster();
}
}
//Main类并不知道PrintBanner类是如何实现的,这样就可以在不用对Main类进行修改的情况下改变PrintBanner类的具体实现
//请求者
public class Main {
public static void main(String[] args){
Print p = new PrintBanner("hello");
p.printStrong();
p.printWeak();
}
}
- 对象适配器模式(使用委托的适配器)
public abstract class Print {
public abstract void printWeak();
public abstract void printStrong();
}
public class PrintBanner extends Print {
private Banner banner;
public PrintBanner(String string) {
this.banner = new Banner(string);
}
@Override
public void printWeak() {
banner.showWithParen();
}
@Override
public void printStrong() {
banner.showWithAster();
}
}
public class Main {
public static void main(String[] args){
Print print = new PrintBanner("hello");
print.printStrong();
print.printWeak();
}
}
-
类适配器模式的类图(使用继承)
-
对象适配器模式的类图(使用委托)
什么时候用Adapter模式?
当现在的类已经被充分测试过了,而且已经被用于其他软件之中时,我们更愿意将这些类作为组件重复使用
可以完全不改变现有代码的前提下使现有代码适配于新的接口
练习
java.util.Properties类可以管理键值对(属性)
使用Adapter模式编写一个将属性保存至文件的FileProperties类,以提供FileIO接口跟main
import java.io.IOException;
public interface FileIO {
public void readFromFile(String filename) throws IOException;
public void writeToFile(String filename) throws IOException;
public void setValue(String key,String value);
public String getValue(String key);
}
import org.junit.Test;
import java.io.IOException;
public class Main {
@Test
public void read(){
FileIO f = new FileProperties();
try {
f.readFromFile("file.txt");
f.setValue("year","2020");
f.setValue("mouth","4");
f.setValue("day","3");
f.writeToFile("newfile.txt");
}catch (IOException e){
e.printStackTrace();
}
}
@Test
public void write(){
FileIO f = new FileProperties();
try {
f.readFromFile("file.txt");
System.out.println("year " + f.getValue("year"));
System.out.println("mouth " + f.getValue("mouth"));
System.out.println("day " + f.getValue("day"));
}catch (IOException e){
e.printStackTrace();
}
}
}
完成代码
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class FileProperties extends Properties implements FileIO {
@Override
public void readFromFile(String filename) throws IOException {
load(new FileInputStream(filename));
}
@Override
public void writeToFile(String filename) throws IOException {
store(new FileOutputStream(filename),"written by FileProperties");
}
@Override
public void setValue(String key, String value) {
setProperty(key,value);
}
@Override
public String getValue(String key) {
return getProperty(key);
}
}