We will summarize the ** Abstract Factory pattern ** in the GoF design pattern.
--Abstract Factory means ** Abstract Factory **. ――Abstract is a state where you are focusing only on the interface without thinking about how it is implemented. --The Abstract Factory pattern does not focus on the concrete implementation of ** parts, but on the interface. And it is a method ** that assembles parts and puts them together in a product using only that interface **. --In the GoF design pattern, it is classified as ** Design pattern for generation **.
A program that outputs a list of favorites in HTML format.
A class that represents an abstract factory. Create Link, Tray and Page.
Factory.java
package factory;
public abstract class Factory {
public abstract Link createLink(String caption, String url);
public abstract Tray createTray(String caption);
public abstract Page createPage(String title);
public static Factory getFactory(String classname) {
Factory factory = null;
try {
factory = (Factory) Class.forName(classname).newInstance();
} catch (ClassNotFoundException e) {
System.err.println("class" + classname + "Cannot be found.");
} catch (Exception e) {
e.printStackTrace();
}
return factory;
}
}
This class handles Link and Tray in a unified manner.
Item.java
package factory;
public abstract class Item {
protected String caption;
public Item(String caption) {
this.caption = caption;
}
public abstract String makeHTML();
}
Abstract part: A class that represents an HTML link.
Link.java
package factory;
public abstract class Link extends Item {
protected String url;
public Link(String caption, String url) {
super(caption);
this.url = url;
}
}
Abstract parts: A class of links and trays.
Tray.java
package factory;
import java.util.ArrayList;
public abstract class Tray extends Item {
protected ArrayList tray = new ArrayList();
public Tray(String caption) {
super(caption);
}
public void add(Item item) {
tray.add(item);
}
}
Abstract part: A class that represents an HTML page.
Page.java
package factory;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
public abstract class Page {
protected String title;
protected ArrayList content = new ArrayList();
public Page(String title) {
this.title = title;
}
public void add(Item item) {
content.add(item);
}
public void output() {
try {
String filename = title + ".html";
Writer writer = new FileWriter(filename);
writer.write(this.makeHTML());
writer.close();
System.out.println(filename + "is created.");
} catch (IOException e) {
e.printStackTrace();
}
}
public abstract String makeHTML();
}
This class represents a specific factory. Create ListLink, ListTray, ListPage.
ListFactory.java
package listfactory;
import factory.Factory;
import factory.Link;
import factory.Page;
import factory.Tray;
public class ListFactory extends Factory {
public Link createLink(String caption, String url) {
return new ListLink(caption, url);
}
public Tray createTray(String caption) {
return new ListTray(caption);
}
public Page createPage(String title) {
return new ListPage(title);
}
}
Specific part: A class that represents an HTML link.
ListLink.java
package listfactory;
import factory.Link;
public class ListLink extends Link {
public ListLink(String caption, String url) {
super(caption, url);
}
public String makeHTML() {
return " <li><a href=\"" + url + "\">" + caption + "</a></li>\n";
}
}
Specific parts: A class that collects Links and Tray.
ListTray.java
package listfactory;
import java.util.Iterator;
import factory.Item;
import factory.Tray;
public class ListTray extends Tray {
public ListTray(String caption) {
super(caption);
}
public String makeHTML() {
StringBuffer buffer = new StringBuffer();
buffer.append("<li>\n");
buffer.append(caption + "\n");
buffer.append("<ul>\n");
Iterator it = tray.iterator();
while (it.hasNext()) {
Item item = (Item) it.next();
buffer.append(item.makeHTML());
}
buffer.append("</ul>\n");
buffer.append("</li>\n");
return buffer.toString();
}
}
Specific part: A class that represents an HTML page.
ListPage.java
package listfactory;
import java.util.Iterator;
import factory.Item;
import factory.Page;
public class ListPage extends Page {
public ListPage(String title) {
super(title);
}
public String makeHTML() {
StringBuffer buffer = new StringBuffer();
buffer.append("<html><head><title>" + title + "</title></head>\n");
buffer.append("<body>\n");
buffer.append("<h1>" + title + "</h1>\n");
buffer.append("<ul>\n");
Iterator it = content.iterator();
while (it.hasNext()) {
Item item = (Item) it.next();
buffer.append(item.makeHTML());
}
buffer.append("</ul>\n");
buffer.append("</body></html>\n");
return buffer.toString();
}
}
This class performs the main processing.
Main.java
import factory.Factory;
import factory.Link;
import factory.Page;
import factory.Tray;
public class Main {
public static void main(String[] args) {
Factory factory = Factory.getFactory("listfactory.ListFactory");
Link qiita = factory.createLink("Qiita", "https://qiita.com//");
Link dot = factory.createLink("Dot install", "https://dotinstall.com/");
Link yahoo = factory.createLink("Yahoo!Japan", "http://www.yahoo.co.jp/");
Link excite = factory.createLink("Excite", "http://www.excite.com/");
Link google = factory.createLink("Google", "http://www.google.com/");
Tray pgTray = factory.createTray("programming");
pgTray.add(qiita);
pgTray.add(dot);
Tray searchTray = factory.createTray("Search site");
searchTray.add(yahoo);
searchTray.add(excite);
searchTray.add(google);
Page page = factory.createPage("favorite");
page.add(pgTray);
page.add(searchTray);
page.output();
}
}
For example, if you want to add a new concrete factory to the sample program, you will create subclasses of Factory, Link, Tray, and Page, and implement the abstract methods of each. In other words, it just embodies the abstract part of the factory package class. At this time, no matter how many concrete factories are added, there is no need to modify the abstract factory.
-** 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