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