[JAVA] Facade pattern

What is the Facade pattern?

Prepare a "window" for processing. As the program grows, more classes are created. To use those classes, understand the relationships between them and You need to call the methods in the correct order. In the Facade pattern, a "window" is prepared, and the user only has to make a request to the "window".

The role of Facade (front)

It will be a simple window for many other roles that make up the system. Providing a high-level and simple interface to the outside.

package facade;

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

public class PageMaker {
	private PageMaker() {

	public static void makeWelcomePage(String mailAddress, String fileName) {
		try {
			Properties mailProp = Database.getProperties("mailData");
			String userName = mailProp.getProperty(mailAddress);
			HtmlWriter writer = new HtmlWriter(new FileWriter(fileName));
			writer.title("Welcome to " + userName + "'s page!");
			writer.paragraph(userName + "Welcome to the page.");
			writer.paragraph("I'm waiting for an email.");
			writer.mailTo(mailAddress, userName);
			System.out.println(fileName + " is created for " + mailAddress + " (" + userName + ")");
		} catch (IOException e) {

Many other roles that make up the system

I do each job, but I'm not aware of the role of Facade. He is called by the Facade role to do his job, but he does not call the Facade role from many other roles.

package facade;

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

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;
package facade;

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("<title>" + title + "</title>");
		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 mailAddress, String userName) throws IOException {
		link("mailTo:" + mailAddress, userName);

	public void close() throws IOException {

The role of Client

Roles that use the Facade pattern

package facade;

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

HTML created by running

スクリーンショット 2020-09-18 14.59.53.png


I used this as a reference. Augmented and Revised Introduction to Design Patterns Learned in Java Language

Recommended Posts

Facade pattern
Design pattern ~ Facade ~
Prototype pattern
Memento Pattern
Mediator pattern
Composite pattern
Observer Pattern
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Visitor Pattern
Bridge pattern
What is the Facade pattern useful for?
abstract Factory Pattern
Design pattern ~ Builder ~
[Java] Strategy pattern
Design pattern ~ Visitor ~
Java design pattern
java callback pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Factory Method Pattern
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
[Java] Singleton pattern
Design pattern ~ Command ~
Abstract Factory pattern
Design pattern ~ Iterator ~
What is Facade? ??
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Template Method pattern
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Factory Method pattern
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
[Java] Adapter pattern
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
Java pattern memo