"Graphic design patterns," the study notes 7-1 facade pattern

Introduction 1. Facade pattern

Program development process, over time, more and more classes, call the relationship will be more complicated, but also issues related to the recall order. At this point we need a "window", the intricate relationships and calling sequence are written, finishing up for users. The key idea of this model is: reduce the difficulty . And thus to improve development efficiency, improve reliability and maintainability of the program.

2. The sample program

The following describes a procedure how to generate a web page: read first from inside the database of user information, and then generates a sequence of pages of head, body, greeting and so on. Although not enough "complex", but also involves two classes have the calling sequence. We want to use this mode, create a window, a line of code to complete all calls.

FIG class 2.1

2.2 Program

As shown in FIG class, Database class is responsible from the database (in this case a text txt) which user information is read, HtmlWriter it contains one API, they need to call in order to generate web pages. PageMaker is a window, it will be the most complex relationships and sequential calls to users packaged Main, Main ultimately only one line of code to complete the work.

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;
    }
}


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();
    }
}


public class PageMaker {
    private PageMaker() {
    }
    public static void makeWelcomePage(String mailaddr, String filename) {
        try {
            //先从数据库中提取出来邮箱信息
            Properties mailprop = Database.getProperties("D:\\XXXXXX\\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();
        }
    }
}


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

//“数据库”
[email protected]=username

3. Roles and class diagrams

  • The Facade (window): down call complex classes and interfaces, provides a simple API upwardly. This role is played by the PageMaker.
  • Other roles constitute the system: each complete their work roles, they do not know the existence of the window, but the window roles will call them. And such HtmlWriter Database class of this embodiment.
  • Client (requestor): responsible for calling Facade role, in this case Main play this role.

4. Notion and Expand

  • Benefits Facade role

The key is to reduce the complexity , if a programmer, it is necessary to care about business logic, but also concerned about a variety of complex underlying API calls the combination - the resulting development will be low efficiency, high error probability. A famous saying, should not Lu said: In the computer world, without adding an intermediate layer not solve the problem, if there is, then recursively added. Facade is the middle layer. The intermediate layer has another advantage: reducing the coupling.

  • Facade pattern recursively

即上文所说,递归的加中间层。

Guess you like

Origin www.cnblogs.com/qianbixin/p/10995785.html