#format java /** * FactoryMethodPattern Example */ import java.util.*; abstract class Product { public abstract void use(); } 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); } class IDCard extends Product { private String owner; IDCard(String owner) { System.out.println(owner+"'s card making"); this.owner = owner; } public void use() { System.out.println(owner+"'s card using"); } public String getOwner() { return owner; } } class IDCardFactory extends Factory { private Vector owners = new Vector(); protected Product createProduct(String owner) { return new IDCard(owner); } protected void registerProduct(Product product) { owners.add(((IDCard)product).getOwner()); } public Vector getOwners() { return owners; } } public class FactoryMethodIdCard { public static void main(String[] args) { Factory factory = new IDCardFactory(); Product card1 = factory.create("Jane"); Product card2 = factory.create("Bob"); card1.use(); card2.use(); } }