[JAVA] Factory Method pattern

Factory Method pattern

In Template Method pattern , the processing framework is created on the superclass side, and the specific processing is fleshed out on the subclass side. The Factory Method pattern is an application of this pattern to the instantiation scene.

The role of Product

An abstract class that defines the interface that an instance generated by this pattern should have. The specific content is determined by the role of Concrete Product.

package factoryMethod;

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

The role of Creator

An abstract class that creates a Product role. The specific content is determined by Contrete Creator. The Creator role doesn't know anything about the Concrete Product role that it actually creates. The only thing the Creator role knows is that if you call the Product role and the method of instantiation, the Product will be created. By replacing the actual instantiation by new with the method call for instantiation, the superclass is released from the binding by the concrete class name.

package factoryMethod;

public abstract class Factory {
	public final Product create(String owner) {
		Product p = createProduct(owner);
		registerProduct(p);
		return p;
	}

	protected abstract Product createProduct(String owner);
	protected abstract void registerProduct(Product product);

}

The role of Concrete Product

Determine specific products.

package factoryMethod;

public class IdCard extends Product {
	private String owner;
	 public IdCard(String owner) {
		 System.out.println(owner + "Make a card");
		 this.owner = owner;
	}

	 @Override
	 public void use() {
		 System.out.println(owner + "Use the card");
	 }

	 public String getOwner() {
		 return owner;
	 }
}

The role of Concrete Creator

Define a class to make a specific product.

package factoryMethod;

import java.util.ArrayList;
import java.util.List;

public class IdCardFactory extends Factory {
	private List<String> owners = new ArrayList<>();

	@Override
	protected Product createProduct(String owner) {
		return new IdCard(owner);
	}

	@Override
	protected void registerProduct(Product product) {
		owners.add(((IdCard)product).getOwner());
	}

	public List<String> getOwners() {
		return owners;
	}
}

Caller

package factoryMethod;

public class Main {

	public static void main(String[] args) {
		Factory factory = new IdCardFactory();
		Product card1 = factory.create("Tanaka");
		Product card2 = factory.create("Suzuki");
		Product card3 = factory.create("Sato");
		card1.use();
		card2.use();
		card3.use();

	}
}

//Execution result
//Make a Tanaka card
//Make a Suzuki card
//Make Sato's card
//I will use Tanaka's card
//I will use Suzuki's card
//I will use Sato's card

A serial number was added to the above IdCard class, and the IdCardFactory class was modified so that it had a correspondence table between the serial number and the owner.

package factoryMethod;

public class IdCard extends Product {
	private String owner;
	private int serial;
	 public IdCard(String owner, int serial) {
		 System.out.println(owner + "(" + serial + ")Make a card");
		 this.owner = owner;
		 this.serial = serial;
	}

	 @Override
	 public void use() {
		 System.out.println(owner + "(" + serial + ")Use the card");
	 }

	 public String getOwner() {
		 return owner;
	 }

	 public int getSerial() {
		 return serial;
	 }
}
package factoryMethod;

import java.util.HashMap;

public class IdCardFactory extends Factory {
	private HashMap<Integer, String> database = new HashMap<>();
	private int serial = 1;


	@Override
	protected synchronized Product createProduct(String owner) {
		return new IdCard(owner, serial++);
	}

	@Override
	protected void registerProduct(Product product) {
		IdCard card = (IdCard)product;
		database.put(card.getSerial(), card.getOwner());
	}

	public HashMap<Integer, String> getDatabase() {
		return database;
	}
}
package factoryMethod;

public class Main {

	public static void main(String[] args) {
		Factory factory = new IdCardFactory();
		Product card1 = factory.create("Tanaka");
		Product card2 = factory.create("Suzuki");
		Product card3 = factory.create("Sato");
		card1.use();
		card2.use();
		card3.use();

	}

}
//Execution result
//Tanaka(1)Make a card
//Suzuki(2)Make a card
//Sato(3)Make a card
//Tanaka(1)Use the card
//Suzuki(2)Use the card
//Sato(3)Use the card


I was able to make modifications without changing the Product class, Factory class, and Main class.

Recommended Posts

Factory Method Pattern
Factory Method pattern
Design pattern ~ Factory Method ~
abstract Factory Pattern
Abstract Factory pattern
Template Method pattern
Template Method Pattern
Java beginner design pattern (Factory Method pattern)
Design pattern ~ Abstract Factory ~
Design pattern ~ Template Method ~
Method
Ruby design pattern template method pattern memo
Introduction to Design Patterns (Factory Method)
C # chewed design pattern: Template Method
Prototype pattern
Memento Pattern
What a Strategy pattern Factory, not a State
Mediator pattern
Iterator pattern
Composite pattern
Java method
to_i method
Observer Pattern
Builder pattern
Bridge Pattern
getRequestDispatcher () method
Command Pattern
merge method
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
include method
Proxy Pattern
Strategy Pattern
Composite Pattern
Abstract method
List method
Singleton pattern
puts method
Java method
Class method
Facade Pattern
getParameter method
[Java] method
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
private method
Facade pattern
Visitor Pattern
rails method
Bridge pattern
[Java] method