概念:
外观模式(Facade),为一套繁杂的子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个接口使得这一子系统更加容易使用。其应用场景
1.为一个复杂子系统提供一个简单接口。
2.提高子系统的独立性,降低耦合度
3.在层次化结构中,可以使用Facade模式定义系统中每一层的入口。
例子:
将基目录下source文件中的字串读取出,然后md5加密,再将加密的结果,输出到目标文件
类图:
FileRead class:
public class FileRead {
private static String TAG = "FileRead";
public String read(String fileName) {
StringBuilder stringBuilder = new StringBuilder();
File psicacheFile = new File(fileName);
if (!psicacheFile.exists()) {
Log.d(TAG, String.format("File:%s does not exists!",fileName));
} else {
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
while (s != null) {
stringBuilder.append(s);
s = br.readLine();
}
br.close();
fr.close();
} catch (IOException ioe) {
Log.v(TAG, ioe.toString());
}
}
Log.d(TAG, String.format("FileRead String: %s", stringBuilder));
return stringBuilder.toString();
}
}
Encryptor加密类:
public class Encryptor {
public final String MD5(String s) {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
try {
byte[] btInput = s.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
FileWrite class:
public class FileWrite {
private static String TAG = "FileWrite";
public void write(String fileName,String string){
File psicacheFile = new File(fileName);
if (!psicacheFile.exists()) {
Log.d(TAG, String.format("File:%s does not exists!",fileName));
try {
psicacheFile.createNewFile();
Log.d(TAG, String.format("File:%s create done!",fileName));
} catch (IOException ioe) {
Log.v(TAG, ioe.toString());
}
}
if (psicacheFile.exists()) {
try {
FileWriter fw = new FileWriter(fileName);
BufferedWriter writer = new BufferedWriter(fw);
writer.write(string);
writer.close();
fw.close();
Log.d(TAG, String.format("File:%s write in %s",fileName,string));
} catch (IOException ioe) {
Log.v(TAG, ioe.toString());
}
}
}
}
Encryptable
public interface Encryptable { public void encrypt(String sourcefile, String outfile); }
EncryptFacade外观类:
public class EncryptFacade implements Encryptable {
private FileRead fileRead;
private Encryptor encryptor;
private FileWrite fileWrite;
@Override
public void encrypt(String sourcefile, String outfile) {
if (null != fileRead && null != encryptor && null != fileWrite) {
fileWrite.write(outfile, encryptor.MD5(fileRead.read(sourcefile)));
}
}
public EncryptFacade() {
fileRead = new FileRead();
encryptor = new Encryptor();
fileWrite = new FileWrite();
}
}
测试类
public class WorkClass {
public void test() {
Encryptable encryptFacade= new EncryptFacade();
encryptFacade.encrypt("/sdcard/source.txt","/sdcard/out.txt"); }}
测试结果:
14:56:18.155 23149-23149/com.example.qinghua_liu.myapplication D/FileRead: FileRead String:facade design14:56:18.155 23149-23149/com.example.qinghua_liu.myapplication D/Encryptor: Encryptor MD5 facade design to 696A9711DC2670D3293E98087F94CACE
14:56:18.157 23149-23149/com.example.qinghua_liu.myapplication D/FileWrite: File:/sdcard/out.txt does not exists!
14:56:18.159 23149-23149/com.example.qinghua_liu.myapplication D/FileWrite: File:/sdcard/out.txt create done!
14:56:18.162 23149-23149/com.example.qinghua_liu.myapplication D/FileWrite: File:/sdcard/out.txt write in 696A9711DC2670D3293E98087F94CACE
结语:
外观模式是为了解决子系统中类与类之间的依赖关系的,像spring一样,可以将类和类之间的关系配置到配置文件中,而外观模式就是将他们的关系放在一个Facade类中,降低了类类之间的耦合度。如果我们没有EncryptFacade类,那么,FileRead、FileWrite、Encryptor他们之间将会相互持有实例,产生关系,这样会造成严重的依赖,修改一个类,可能会带来其他类的修改,这不是我们想要看到的,有了EncryptFacade类,他们之间的关系被放在了EncryptFacade类里,这样就起到了解耦的作用,这也是外观模式的意义。