[JAVA] Design pattern ~ Abstract Factory ~

1.First of all

We will summarize the ** Abstract Factory pattern ** in the GoF design pattern.

2. What is the Abstract Factory 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 **.

3. Sample class diagram

AbstractFactory.PNG

4. Sample program

A program that outputs a list of favorites in HTML format.

4-1. Factory class

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

4-2. Item class

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

4-3. Link class

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

4-4. Tray class

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

4-5. Page class

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

4-6. ListFactory class

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

4-7. ListLink class

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

4-8. ListTray class

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

4-9. ListPage class

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

4-10. Main class

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

4-11. Execution result

キャプチャ.PNG

5. Benefits

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.

  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 ~ Abstract Factory ~
abstract Factory Pattern
Abstract Factory pattern
Design pattern ~ Factory Method ~
Introduction to Design Patterns (Abstract Factory)
Java beginner design pattern (Factory Method pattern)
Design pattern ~ Builder ~
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Factory Method Pattern
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 ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Factory Method pattern
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Advantages of generating an Abstract Factory pattern with an enum
Design pattern ~ Chain of Responsibility ~
[Design pattern] Java core library
Ruby design pattern template method pattern memo
Introduction to Design Patterns (Factory Method)
Design patterns to enjoy with frequently used Java libraries --Factory pattern
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
What a Strategy pattern Factory, not a State
[Design pattern] Common logic with Template Method