天天看点

设计模式(十五)-外观模式(Facade Pattern)——推卸责任

外观模式(Facade Pattern)可以为互相关联在一起的错综复杂的类整理出高层接口。

Database.java

package com.test.dp.Facade.Sample.pagemaker;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

//从邮件地址中获取用户名的类
public class Database {
    private Database() {    // 防止外部new出Database的实例,所以声明为private方法
    }
    public static Properties getProperties(String dbname) { // 根据数据库名获取Properties
        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

[email protected]=Hiroshi Yuki
[email protected]=Hanako Sato
[email protected]=Tomura
[email protected]=Mamoru Takahashi
           

HtmlWriter.java

package com.test.dp.Facade.Sample.pagemaker;

import java.io.Writer;
import java.io.IOException;

//编写HTML文件的类
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>\n");
    }
    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:" + mailaddr, username);
    }
    public void close() throws IOException {    // 结束输出HTML
        writer.write("</body>");
        writer.write("</html>\n");
        writer.close();
    }
}
           

PageMaker.java

package com.test.dp.Facade.Sample.pagemaker;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

//根据邮件地址编写用户的Web页面
public class PageMaker {
    private PageMaker() {   // 防止外部new出PageMaker的实例,所以声明为private方法
    }
    public static void makeWelcomePage(String mailaddr, String filename) {
        try {
            Properties mailprop = Database.getProperties("maildata");
            String username = mailprop.getProperty(mailaddr);
            HtmlWriter writer = new HtmlWriter(new FileWriter(filename));
            writer.title("Welcome to " + username + "'s page!");
            writer.paragraph("欢迎来到" + username + "的主页。");
            writer.paragraph("等着你的邮件哦!");
            writer.mailto(mailaddr, username);
            writer.close();
            System.out.println(filename + " is created for " + mailaddr + " (" + username + ")");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
           

Main.java

package com.test.dp.Facade.Sample;

import com.test.dp.Facade.Sample.pagemaker.PageMaker;

public class Main {
    public static void main(String[] args) {
        PageMaker.makeWelcomePage("[email protected]", "welcome.html");
    }
}
           

执行结果:

总结:

应用实例:1、去医院看病,可能要去挂号、门诊、划价、取药,让患者或患者家属觉得很复杂,如果有提供接待人员,只让接待人员来处理,就很方便。2、JAVA 的三层开发模式。

优点:1、减少系统相互依赖。2、提高灵活性。3、提高了安全性。

缺点:不符合开闭原则,如果要改东西很麻烦,继承重写都不合适。

使用场景:1、为复杂的模块或子系统提供外界访问的模块。2、子系统相对独立。3、预防低水平人员带来的风险。

注意事项:在层次化结构中,可以使用外观模式定义系统中每一层的入口。