Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
// Factories package com.apwebco.patterns.gof.abstractfactory; public abstract class FinancialToolsFactory { public abstract TaxProcessor createTaxProcessor(); public abstract ShipFeeProcessor createShipFeeProcessor(); } public class CanadaFinancialToolsFactory extends FinancialToolsFactory { public TaxProcessor createTaxProcessor() { return new CanadaTaxProcessor(); } public ShipFeeProcessor createShipFeeProcessor() { return new CanadaShipFeeProcessor(); } } public class EuropeFinancialToolsFactory extends FinancialToolsFactory { public TaxProcessor createTaxProcessor() { return new EuropeTaxProcessor(); } public ShipFeeProcessor createShipFeeProcessor() { return new EuropeShipFeeProcessor(); } } // Products public abstract class ShipFeeProcessor { abstract void calculateShipFee(Order order); } public abstract class TaxProcessor { abstract void calculateTaxes(Order order); } public class EuropeShipFeeProcessor extends ShipFeeProcessor { public void calculateShipFee(Order order) { // insert here Europe specific ship fee calculation } } public class CanadaShipFeeProcessor extends ShipFeeProcessor { public void calculateShipFee(Order order) { // insert here Canada specific ship fee calculation } } public class EuropeTaxProcessor extends TaxProcessor { public void calculateTaxes(Order order) { // insert here Europe specific taxt calculation } } public class CanadaTaxProcessor extends TaxProcessor { public void calculateTaxes(Order order) { // insert here Canada specific taxt calculation } } // Client public class OrderProcessor { private TaxProcessor taxProcessor; private ShipFeeProcessor shipFeeProcessor; public OrderProcessor(FinancialToolsFactory factory) { taxProcessor = factory.createTaxProcessor(); shipFeeProcessor = factory.createShipFeeProcessor(); } public void processOrder (Order order) { // .... taxProcessor.calculateTaxes(order); shipFeeProcessor.calculateShipFee(order); // .... } } // Integration with the overall application public class Application { public static void main(String[] args) { // ..... String countryCode = "EU"; Customer customer = new Customer(); Order order = new Order(); OrderProcessor orderProcessor = null; FinancialToolsFactory factory = null; if (countryCode == "EU") { factory = new EuropeFinancialToolsFactory(); } else if (countryCode == "CA") { factory = new CanadaFinancialToolsFactory(); } orderProcessor = new OrderProcessor(factory); orderProcessor.processOrder(order); } }