Design Pattern | Facade Pattern

What Is the Facade Pattern?

  • The English word facade means front.
  • It means the front of a building and refers to a design pattern that provides a simplified interface to another large part of a software codebase.
  • To perform processing using a large program, many related classes must be controlled appropriately. If a gateway for that processing is prepared, many classes do not need to be controlled individually.
  • The Facade pattern prepares a simple gateway to a complex system.
  • A Facade object reduces the dependency of code outside complex software on the internal code of a library and provides a simple interface for using the complex software.
  • In GoF design patterns, it is classified as a structural design pattern.

Facade Pattern Example Program

This example program creates a user’s web page.

Class Diagram
Facade Pattern Class Diagram

1. PageMaker Class

This class creates a user’s web page from an email address. This class is the Facade.

PageMaker.java

package com.devkuma.designpattern.structural.facade.pagemaker;

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

public class PageMaker {

    private PageMaker() {}

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

2. HtmlWriter Class

This class writes an HTML file.

HtmlWriter.java

package com.devkuma.designpattern.structural.facade.pagemaker;

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

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 {
        writer.write("</body>");
        writer.write("</html>\n");
        writer.close();
    }
}

3. Database Class

This class obtains a username from an email address.

Database.java

package com.devkuma.designpattern.structural.facade.pagemaker;

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

public class Database {

    private Database() {
    }

    public static Properties getProperties(String dbname) {
        ClassLoader loader = Database.class.getClassLoader();
        String file = loader.getResource("facade/pagemaker/" + dbname + ".txt").getFile();
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream(file));
        } catch (IOException e) {
            System.out.println("Warning: " + file + " is not found.");
        }
        return prop;
    }
}

4. maildata Database

This is a database file.

maildata.txt

devkuma@devkuma.com=dekuma
ariakuma@devkuma.com=ariakuma
kimkc@devkuma.com=kim

5. Main Class

This is the class that executes the main processing.

Main.java

package com.devkuma.designpattern.structural.facade;

import com.devkuma.designpattern.structural.facade.pagemaker.PageMaker;

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

6. Execution Result

<html><head><title>Welcome to devkuma's page!</title></head><body>
<h1>Welcome to devkuma's page!</h1>
<p>devkuma의 페이지에 어서오세요.</p>
<p>문의 사항을 메일을 보내주세요.</p>
<p><a href="mailto:devkuma@devkuma.com">devkuma</a></p>
</body></html>

Advantages of the Facade Pattern

When there are many classes or methods, programmers may be unsure which one to use or must pay attention to call order. The need to pay attention means mistakes become more likely.
Using the Facade pattern reduces the interface and presents complex things simply.
Having fewer interfaces can also be described as looser coupling with the outside. In other words, it creates loose coupling and makes packages easier to reuse as components.