Creational Patterns  «Prev 

Abstract Factory Pattern Code in Java

If you need an additional level of abstraction over your Factory pattern than the Abstract Factory Pattern is the right design pattern to use. The Abstract Factory allows you to use different Factory implementations for different purposes. One of the best examples of the Abstract Factory and Factory pattern in Java is DocumentBuilderFactory and DocumentBuilder javax.xml.parsers package.
The following code is an example of the Abstract Factory Pattern.

// Factories
package com.java.creational.abstractfactory;
public abstract class FinancialToolsFactory { public abstract TaxProcessor createTaxProcessor(); public abstract ShipFeeProcessor createShipFeeProcessor(); } class CanadaFinancialToolsFactory extends FinancialToolsFactory { public TaxProcessor createTaxProcessor() { return new CanadaTaxProcessor(); } public ShipFeeProcessor createShipFeeProcessor() { return new CanadaShipFeeProcessor(); } } class EuropeFinancialToolsFactory extends FinancialToolsFactory { public TaxProcessor createTaxProcessor() { return new EuropeTaxProcessor(); } public ShipFeeProcessor createShipFeeProcessor() { return new EuropeShipFeeProcessor(); } }

package com.java.creational.abstractfactory;

public abstract class ShipFeeProcessor {
  abstract void calculateShipFee(Order order);
}
abstract class TaxProcessor {
  abstract void calculateTaxes(Order order);
}
class EuropeShipFeeProcessor extends ShipFeeProcessor {
  public void calculateShipFee(Order order) {
    // insert here Europe specific ship fee calculation
  }
}	
class CanadaShipFeeProcessor extends ShipFeeProcessor {
  public void calculateShipFee(Order order) {
    // insert here Canada specific ship fee calculation
  }
}
class EuropeTaxProcessor extends TaxProcessor {
  public void calculateTaxes(Order order) {
    // insert here Europe specific taxt calculation
  }
}
class CanadaTaxProcessor extends TaxProcessor {
  public void calculateTaxes(Order order) {
    // insert here Canada specific taxt calculation
  }
}

package com.java.creational.abstractfactory;
// 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);
    // ....
  }
}

package com.java.creational.abstractfactory;
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); } }

// ----- Customer
package com.java.creational.abstractfactory;
import java.util.Date;
public class Customer {
  private String firstName;
  private String lastName;
  private Date brithDate;
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName; 
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public Date getBrithDate() {
    return brithDate;
  }
  public void setBrithDate(Date brithDate) {
    this.brithDate = brithDate;
  }		
}

// ------ Order
package com.java.creational.abstractfactory;
public class Order {
  private char[] partNumber;
  private String location;
  private String cost;
}