[JAVA] Design pattern ~ Factory Method ~

1.First of all

Here is a summary of the ** Factory Method pattern ** in the GoF design pattern.

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

3. Sample class diagram

FactoryMethod.PNG

4. Sample program

This is a program to create an ID card from an ID card factory.

4-1. Factory class

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

4-2. Product class

This is the base class of the object generated by Factory.

Product.java


package framework;

public abstract class Product {
    public abstract void use();
}

4-3. IDCardFactory class

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

4-4. IDCard class

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

4-5. Main class

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

4-6. Execution result

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.

5. Benefits

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.

  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 ~ Factory Method ~
Factory Method Pattern
Factory Method pattern
Java beginner design pattern (Factory Method pattern)
Design pattern ~ Abstract Factory ~
Design pattern ~ Template Method ~
Ruby design pattern template method pattern memo
Introduction to Design Patterns (Factory Method)
C # chewed design pattern: Template Method
abstract Factory Pattern
Design pattern ~ Builder ~
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 ~
Abstract Factory pattern
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Template Method pattern
Design pattern ~ Decorator ~
Template Method Pattern
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
[Design pattern] Common logic with Template Method
C ++ design pattern (TemplateMethod pattern)
static factory method part 1
GoF design pattern summary
Java design pattern summary
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Design pattern ~ Chain of Responsibility ~
Item 51: Design method signatures carefully
[Design pattern] Java core library
Method
Design patterns to enjoy with frequently used Java libraries --Factory pattern
Introduction to Design Patterns (Abstract Factory)
Application example of design pattern (No. 1)