Abstract Factory Pattern

Intent:
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Applicability:

  • Use this when a system should be independent of how is product are created, composed and represented.
  • A system should be configured with one of multiple families of products.

Also Known As: Kit

This pattern is one level of abstraction higher than Factory Method Pattern. The abstract factory returns the factory of classes not the classes themselves unlike the factory pattern. Abstract Factories can be implemented using the Factory Method pattern and implementations (concrete factories) are usually singleton. Another approach would be to use the Prototype pattern. In Java, an abstract factory can be designed as an abstract class or an interface with its concrete subclasses as factories, where each factory is responsible for creating and providing access to the objects of a particular suite of classes. Client objects do not have to know about the existence of these concrete factory classes.

Structure

abstract_factory.jpg

Example:

Suppose we have a customer data management application where we have basic functionalities is to validate and save the input customer data consisting of account, address and credit card data. The application should be functional in both local and remote modes so for the Account interface we have RemoteAccount and LocalAccount, etc. Our goal is to create instances of Account, Address and CreditCard objects in both modes.

We design an interface in the form of the CustomerFactory Java interface, which declares the methods to create instances of local or remote family of customer data classes (Account, Address and CreditCard).

customer_tree.jpeg
abstract_factory1.jpg

Consequences:

  • Defining a new kind of product is difficult since the interface of Abstract Factory should be changed. If the creator method of the interface can work with a parameter then this might be easier.
public interface Account {
    public boolean isValid();
    public boolean save();
    public String getFirstName();
    public String getLastName();
}
 
public class LocalAccount implements Account {
...
}
public class RemoteAccount implements Account {
...
}
 
public interface CustomerFactory {
    Address getAddress();
    Account getAccount();
    CreditCard getCreditCard();
}
 
public class LocalCustomerFactory implements CustomerFactory {
...
}
 
public class RemoteCustomerFactory implements CustomerFactory {
...
}
 
public class Client {
    public static void main(String[] args) {
        CustomerFactory cf = getCustomerFactory("local");
        cf.getAccount();
    }
 
    private static CustomerFactory getCustomerFactory(String mode) {
        if (mode=="local") return new LocalCustomerFactory();
        return new RemoteCustomerFactory();
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License