Design pattern ~ Facade ~

1.First of all

We will summarize the ** Facade pattern ** in the GoF design pattern.

2. What is the Facade pattern?

--The English word Facade means ** front **. ――In order to process with a large program, you have to properly control many related classes. If you have a window to handle that, you don't have to control many classes individually. --The Facade pattern is a ** method that provides a simple window for complex systems **. --The GoF design patterns are classified as ** structural design patterns **.

3. Sample class diagram

Facade.PNG

4. Sample program

A program that creates a user's web page.

4-1. PageMaker class

A class that creates a user's web page from an email address. This class becomes Facade.

PageMaker.java


package 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 + "Welcome to the page.");
			writer.paragraph("You have an email.");
			writer.mailto(mailaddr, username);
			writer.close();
			System.out.println(filename + " is created for " + mailaddr + " (" + username + ")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

4-2. HtmlWriter class

A class that creates an HTML file.

v.java


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

4-3. Database class

This class gets the user name from the email address.

Database.java


package pagemaker;

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

4-4. maildata database

This is a database file.

maildata.txt


[email protected]=Taro Tanaka
[email protected]=Hanako Yamada
[email protected]=Daisuke Suzuki

4-5. Main class

This class performs the main processing.

Main.java


import pagemaker.PageMaker;

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

4-6. Execution result

f.PNG

5. Benefits

With so many classes and methods, programmers have to wonder which one to use and be careful about the order in which they are called. Being careful means that it is easy to make a mistake. By using the Facade pattern, you can ** reduce the number of interfaces and make complex things look simple **. A small number of interfaces can also be expressed as a loose connection with the outside. In other words, it becomes a loose bond and makes it easy to reuse the package as a part.

  1. GitHub

7. List of design patterns

-** GoF design pattern summary **

8. Reference

This article and sample program were created based on the following books.

-** Introduction to design patterns learned in Java language **

It was very easy to understand and I learned a lot. Thank you. The detailed explanations of the design patterns and sample programs are written, so please take a look at the books as well.

Recommended Posts

Design pattern ~ Facade ~
Facade Pattern
Facade pattern
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Factory Method ~
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Design pattern ~ Chain of Responsibility ~
[Design pattern] Java core library
Ruby design pattern template method pattern memo
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Mediator pattern
Iterator pattern
Composite pattern
Observer Pattern
Builder pattern
Bridge Pattern
Command Pattern
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
[Design pattern] Common logic with Template Method
What is the Facade pattern useful for?
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Visitor Pattern