0% found this document useful (0 votes)
33 views18 pages

Design Patterns: Abstract Factory Pattern

The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It defines an interface for creating abstract product objects that are part of a family, but leaves the concrete product class determination to subclasses. This allows clients to work with instances of a family of products without depending on their concrete classes. For example, a financial tools factory could create different tax and shipping fee processors depending on the country, hiding such logic from clients.

Uploaded by

sayy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views18 pages

Design Patterns: Abstract Factory Pattern

The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It defines an interface for creating abstract product objects that are part of a family, but leaves the concrete product class determination to subclasses. This allows clients to work with instances of a family of products without depending on their concrete classes. For example, a financial tools factory could create different tax and shipping fee processors depending on the country, hiding such logic from clients.

Uploaded by

sayy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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