代码Github连接 :https://github.com/tangbi123/Tab-Design-Pattern
Facade
可以为互相关联在一起地错综复杂地类整理出高层接口(API),让系统对外只有一个简单的接口(API),并且考虑系统内部各个类之间地责任关系和依赖关系,按照正确地顺序调用各个类。
示例
文件结构
代码清单
1)Database
public class Database {
private Database(){
}
public static Properties getProperties(String dbname){
String filename = dbname + ".txt";
Properties prop = new Properties();
try{
prop.load(new FileInputStream(filename));
}catch (IOException e){
System.out.println("warning:" + filename + "is not found.");
}
return prop;
}
}
mailData.txt
hyuki@hyuki.com=Hiroshi Yuki
hanako@hyuki.com=Hanako Sato
tomura@hyuki.com=Tomura
mamoru@hyuki.com=Mamoru Takahashi
2)HtmlWriter
public class HtmlWriter {
private Writer writer;
public HtmlWriter(Writer writer) {
this.writer = writer;
}
public void title(String title)throws IOException{
writer.write("<html>");
writer.write("<head>");
writer.write("<title>" + title + "</title>");
writer.write("</head>");
writer.write("<body>\n");
writer.write("<h1>" + title + "</h1>\n");
}
public void paragraph(String msg) throws IOException {
writer.write("<p>" + msg + "</p>");
}
public void link(String href, String caption)throws IOException{
paragraph("<a href = \"" + href + "\">"
+ caption + "</a>");
}
public void mailto(String mailaddr, String username)throws IOException{
link("mailto:"