We will summarize the ** Facade pattern ** in the GoF design 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 **.
A program that creates a user's web page.
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();
}
}
}
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();
}
}
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;
}
}
This is a database file.
maildata.txt
[email protected]=Taro Tanaka
[email protected]=Hanako Yamada
[email protected]=Daisuke Suzuki
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");
}
}
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.
-** GoF design pattern summary **
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