可以利用Facade提供單一介面,整合這些要做的功能
讓使用者只要透過呼叫Facade所提供的單一介面窗口
就可執行整個複雜的工作內容
例如,希望載入外部資料庫(DataBase),並利用這些載入的資料,
產生網頁(HtmlWriter),這工作
參與者
1.DataBase
用來載入外部資料的類別
public class Database {
private Database(){}
public static Properties getProperties(String dbName){
String fileName=dbName;
Properties prop=new Properties();
prop.load(new FileInputStream(fileName));
return prop;
}
}
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><head>");
writer.write("<title>"+title+"</title");
writer.write("</head><body>\n");
writer.write("<h1>"+title+"</h1>\n");
}
public void close()throws IOException{
writer.write("</body></html>");
writer.close();
}
}
3.Facade
用來整合功能的介面
public class Facade {
private Facade(){}
public static void makeWelcomePage(String dbFileName,String addr, String outFileName){
Properties mailProp=Database.getProperties(dbFileName);
String userName=mailProp.getProperty(addr);
HtmlWriter writer=new HtmlWriter(new FileWriter(outFileName));
writer.title("Welcome "+userName);
writer.close();
}
}
4.Main
外部程式
Facade.makeWelcomePage("d:\\list.txt","xxx@xxx.com","d:\\newPage.htm");
沒有留言:
張貼留言