Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

DESIGN PATTERNS

ABSTRACT FACTORY PATTERN


DEFINITION

 Provides an interface for creating


families of related or dependent objects
without specifying their concrete classes.
 Non-Software Analog

English tool box (inches,…) metric tool box (cm, mm,……)


Class Diagram
Example
Abstract Products
public abstract class ShipFeeProcessor {
abstract void calculateShipFee(Order order);
}

public abstract class TaxProcessor {


abstract void calculateTaxes(Order order);
}
Concrete products
public class CanadaShipFeeProcessor extends
ShipFeeProcessor {
public void calculateShipFee(Order order) {
// insert here Canada specific ship fee calculation
}
}
public class CanadaTaxProcessor extends TaxProcessor
{ public void calculateTaxes(Order order) {
// insert here Canada specific taxt calculation
}
}
Example
Abstract Factory
public abstract class FinancialToolsFactory {
public abstract TaxProcessor createTaxProcessor();
public abstract ShipFeeProcessor
createShipFeeProcessor();
}
Concrete factories
public class CanadaFinancialToolsFactory extends
FinancialToolsFactory {
public TaxProcessor createTaxProcessor() {
return new CanadaTaxProcessor();
}
public ShipFeeProcessor createShipFeeProcessor() {
return new CanadaShipFeeProcessor();
}
}
Example
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);
// ....
}
}
Issues hidden from the client

 The number of sets of instances


supported by the system
 Which set is currently in use
 The concreate types that are instantiated
at any point
 The issue upon which the sets vary
Integration with main application
public class Application {
public static void main(String[] args) {
// ..... String countryCode = “CA";
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);
}
}
Factory factory????
public static FinancialToolsFactory getAFtoUse(String
customerCode){
if(customerCode.startsWith(“EU")){
return new EuropeFinancialToolsFactory();
} else {
return new CanadaFinancialToolsFactory();
}
}
Implementation options

 Option 1:
 SingleConcreteFactory implemented using
a procedural approach.
 Drawback: Not ideally object oriented
Option 2: database
TaxProcessor createTaxProcessor () {
String query = “SELECT CALC_TAX FROM mytable
WHERE ID = " + ID;
ResultSet myResults =getQueryResults(query);
String classToInstantiate;
classToInstantiate=
myResults.getString("CALC_TAX");
return Class.forName(classToInsantiate);
}
Consequent Forces
 Testing
 testcan use type-checking to determine that
the proper concrete types are created under
the right set of circumstances
 Cost benefits
 Flexible for adding a new family or changing
the implementation of present
 But if an entirely new abstract concept
enters, eg trade restrictions to a new
country
References

 http://www.apwebco.com/gofpatterns/creation
 http://www.netobjectivesrepository.com/TheA
 Questions????
 Thank you!

You might also like