Abstract Factory pattern

Factories have been a key pattern in building applications, its fascinatingly simple, effective and to the point. When starting to learn a design oriented approach to applications or API, I would always recommend a factory pattern as one of the key starting notes of highlight in your design.

So today I am talking about the Abstract Factory pattern. Its not an “abstract” class or object that you call a pattern. But its a Factory of facotries and that is what exactly makes it so much wordingly abstract. Having “abstract” classes is there but just some other side of the coin.

When should I use an Abstract Factory:

  • Independence of how products are created, composed or represented
  • Should be configurable with one of the multiple families or products
  • You need enforcable constraints for the products used as a group
  • You need to reveal only the interfaces of products and not thier implementation as part of a bigger picture.

So lets begin with the fun.

This is how I plan to implement it:
Has A:
Product has a Specification
Factory has a Product
FactoryManager has FactoryConstants
FactoryManager has ComputerFactory

Is A:
BFactory is a ComputerFactory
AFactory is a ComputerFactory

Not shown.
ProductA is a Product
ProductB is a Product

Diagram:

AbstractFactory
AbstractFactory

Creating a simple factory that returns products.

  1. public abstract class ComputerFactory {
  2.  
  3.  public abstract String getName();
  4.  
  5.  public abstract Product[] getProducts();
  6.  
  7.  public abstract Product getProduct(int ProductID);
  8.  
  9. }

Implementation of the ComputerFactory

  1. public class AFactory extends ComputerFactory {
  2.  
  3. public String getName(){
  4. return "A";
  5. }
  6.  
  7. public Product[] getProducts(){
  8. return null;
  9. }
  10.  
  11. public Product getProduct(int productID){
  12. switch(productID){
  13. case 1:
  14. return new ProductA();
  15.  
  16. case 2:
  17. return new ProductB();
  18.  
  19. default:
  20. throw new IllegalArgumentException("Sorry you hit the wrong factory, we closed down in 1600 BC");
  21. }
  22. }
  23. }

A register base for factories. Refer to the main method for use later in this post.

  1. public interface FactoryConstants {
  2.  
  3.  public int A = 1;
  4.  public int B = 2;
  5.  
  6. }

The main Entrant class. the Factory Manager that will give the ComputerFactory resultant. Its assumed to be a Singleton as it registers as a Creator in the system (assumption).

  1. public class FactoryManager{
  2.  
  3.  private static FactoryManager factoryManager = null;
  4.  
  5.  private FactoryManager(){
  6.  
  7.  }
  8.  
  9.  public static FactoryManager getInstance(){
  10.   if(factoryManager != null){
  11.    return factoryManager;
  12.   }
  13.   else return factoryManager = new FactoryManager();
  14.  }
  15.  
  16.  public ComputerFactory getFactory(int factory) throws IllegalArgumentException{
  17.  
  18.   switch(factory){
  19.    case FactoryConstants.A:
  20.    return new IBMFactory();
  21.  
  22.    case FactoryConstants.B:
  23.    return new SUNFactory();
  24.  
  25.    default:
  26.    throw new IllegalArgumentException("Sorry you hit the wrong factory, we closed down in 1600 BC");
  27.   } 
  28.  }
  29. }

A main method to test the AbstractFactory

  1.  public static void main(String args[]){
  2.  
  3.   System.out.println(FactoryManager.getInstance().getFactory(FactoryConstants.A).getName());
  4.   System.out.println(FactoryManager.getInstance().getFactory(FactoryConstants.B).getName());
  5.   System.out.println(FactoryManager.getInstance().getFactory(3).getName());
  6.  
  7.  }

You can find the complete code listing here:
AbstractFactory source

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">