Design Patterns Part – 2

Abstract Factory

A factory class helps us to centralize the creation of classes and types. Abstract factory helps us to bring uniformity between related factory patterns which leads more simplified interface for the client. Abstract factory expands on the basic factory pattern. Abstract factory helps us to unite similar factory pattern classes in to one unified interface. So basically all the common factory patterns now inherit from a common abstract factory class which unifies them in a common class.

In the Implementation phase,Authors discuss about the idea of defining extensible factories. An Abstract Factory is composed of Factory Methods, and each Factory Method has only one signature. Abstract Factory class provides an interface for creating families of related or dependent objects without specifying their concrete classes. We would have to specify different concrete factory subclasses in order to create an object in multiple ways.

Figure: – Abstract factory unifies related factory patterns

Now that we know the basic lets try to understand the details of how abstract factory patterns are actually implemented. As said previously we have the factory pattern classes (factory1 and factory2) tied up to a common abstract factory (Abstract Factory Interface) via inheritance. Factory classes stand on the top of concrete classes which are again derived from common interface.

Figure: – Implementation of abstract factory

For instance in figure ‘Implementation of abstract factory’ both the concrete classes ‘product1’ and ‘product2’ inherits from one interface i.e. ‘common’. The client who wants to use the concrete class will only interact with the abstract factory and the common interface from which the concrete classes inherit.

Now let’s have a look at how we can practically implement abstract factory in actual code. We have scenario where we have UI creational activities for textboxes and buttons through their own centralized factory classes ‘jpFactoryButton’ and ‘jpFactoryText’. Both these classes inherit from common interface ‘InterfaceRender’.

Figure: – Example for abstract factory

Both the factories ‘jpFactoryButton’ and ‘jpFactoryText’ inherits from the common factory ‘jpAbstractFactory’. Figure ‘Example for AbstractFactory’ shows how these classes are arranged and the client code for the same. One of the important points to be noted about the client code is that it does not interact with the concrete classes. For object creation it uses the abstract factory ( jpAbstractFactory ) and for calling the concrete class implementation it calls the methods via the interface ‘InterfaceRender’. So the ‘jpAbstractFactory’ class provides a common interface for both factories ‘jpFactoryButton’ and ‘jpFactoryText’.

We will just run through the sample code for abstract factory. Below code snippet ‘Abstract factory and factory code snippet’ shows how the factory pattern classes inherit from abstract factory.

Figure: – Abstract factory and factory code snippet

Figure: – Common interface for concrete classes

Figure ‘Common Interface for concrete classes’ how the concrete classes inherits from a common interface ‘InterFaceRender’ which enforces the method ‘render’ in all the concrete classes.

The final thing is the client code which uses the interface ‘InterfaceRender’ and abstract factory ‘jpAbstractFactory’ to call and create the objects. One of the important points about the code is that it is completely isolated from the concrete classes. Due to this any changes in concrete classes like adding and removing concrete classes does not need client level changes.

Figure: – Client, interface and abstract factory

The Abstract Factory is applied when Pattern Factory of factories are to be dynamically customizable. Abstract class of factory are implemented when different types of factories are required. e.g. factory class to create controls for individual OS.

Abstract Factory entities should  create objects at the time of creation of objects not at the time of factory creation or not during the execution of factory constructor. Sample code shows how factories are created dynamically not at abstract factory instantiation time.

Public abstract class connectionFactory {



SecureFactory extends connectionFactory {

connectionFactory create(String type) {

if (type.equals(‘Oracle’)

return new OracleConnection();


return new SQLServer();

else return new MySQL();



The next blog in the series I will be explaining about Adapter Design Pattern. Please send me the feedback on this blog so that I can improve the future blog contents.

Digg This

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s