Here is a summary of the ** Factory Method pattern ** in the GoF design pattern.
--The English word Factory means ** factory **. --The Factory Method pattern is a method in which the method of creating an instance is defined by the superclass, and the specific generation process is performed by the subclass side **. --The Factory Method pattern is the factory that creates the instance and is configured with the Tmplate Method pattern. --In the GoF design pattern, it is classified as ** Design pattern for generation **.

This is a program to create an ID card from an ID card factory.
This is the base class of Factory. Determine how to create an instance.
Factory.java
package framework;
public abstract class Factory {
	public final Product create(String owner) {
        Product product = createProduct(owner);
        registerProduct(product);
        return product;
    }
	protected abstract Product createProduct(String owner);
    protected abstract void registerProduct(Product product);
}
This is the base class of the object generated by Factory.
Product.java
package framework;
public abstract class Product {
    public abstract void use();
}
A concrete class that implements the methods defined in the Factory class.
IDCardFactory.java
package idcard;
import java.util.ArrayList;
import framework.Factory;
import framework.Product;
public class IDCardFactory extends Factory {
	private ArrayList<String> owners = new ArrayList<String>();
	protected Product createProduct(String owner) {
        return new IDCard(owner);
    }
	protected void registerProduct(Product product) {
		IDCard icCard = (IDCard)product;
		String owner = icCard.getOwner();
		owners.add(owner);
    }
	public ArrayList<String> getOwners() {
        return owners;
    }
}
A concrete class that implements the methods defined in the Product class.
IDCard.java
package idcard;
import framework.Product;
public class IDCard extends Product {
	private String owner;
	IDCard(String owner) {
        System.out.println(owner + "Make a card.");
        this.owner = owner;
    }
	public void use() {
        System.out.println(owner + "Use the card.");
    }
	public String getOwner() {
        return owner;
    }
}
This class performs the main processing.
Main.java
import framework.*;
import idcard.*;
public class Main {
    public static void main(String[] args) {
        Factory factory = new IDCardFactory();
        Product card1 = factory.create("Yamada");
        Product card2 = factory.create("Suzuki");
        Product card3 = factory.create("Sato");
        card1.use();
        card2.use();
        card3.use();
    }
}
Make Yamada's card.
I will make a Suzuki card.
Make Sato's card.
I will use Yamada's card.
I will use Suzuki's card.
I will use Sato's card.
Factory / Product is in the framework package, and IDCardFactory / IDCard is in the idcard package. The framework package does not import the idcard package. In other words, the ** framework package has a ** form that does not depend on the idcard package. If you try to create a completely different "product" and "factory", you don't have to modify the contents of the framework package.
-** 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