Abstract Factory Pattern

Definition

Provides one level of interface higher than the factory pattern. It is used to return one of several factories.

Where to use & benefits

  • Creates families of related or dependent objects like Kit.
  • Provides a class library of products, exposing interface not implementation.
  • Needs to isolate concrete classes from their super classes.
  • A system needs independent of how its products are created, composed, and represented.
  • Try to enforce a constraint.
  • An alternative to Facade to hide platform-specific classes
  • Easily extensible to a system or a family

 

Factory of factories. To keep things simple you can understand it like, you have a set of ‘related’ factory method design pattern. Then you will put all those set of simple factories inside a factory pattern. So in turn you need not be aware of the final concrete class that will be instantiated. You can program for the interface using the top factory.

There is also a view that abstract factory is ‘also’ implemented using prototype instead of factory methords pattern. Beginners for now please don’t yourself with that. Just go with factory methods pattern.

As there is a word ‘abstract’ in the pattern name don’t mistake and confuse it with java ‘abstract’ keyword. It is not related to that. This abstract is from object oriented programming paradim.

Sample abstract factory design pattern implementation in Java API

XML API implements abstract factory. There is a class name SchemaFactory. This acts as a factory and supports implemenation of multiple schemas using abstract factory design pattern.

Sample Java Source Code for Factory Method Design Pattern

Following is the interface, that will be returned as the final end product from the factories.


package com.javapapers.sample.designpattern.abstractfactory;

public interface Animal {
 public void breathe();
}

Following is the interface for which the factory implementation should be done. Inturn all abstract factory will return this type.


package com.javapapers.sample.designpattern.abstractfactory;

public interface AnimalFactory {
 public Animal createAnimal();
}

One of the factory from a predefined set which will instantiate the above interface.


package com.javapapers.sample.designpattern.abstractfactory;

public class SeaFactory implements AnimalFactory {

 public Animal createAnimal() {
 return new Shark();
 }

}

Second factory from a predefined set which will instantiate the Animal interface.


package com.javapapers.sample.designpattern.abstractfactory;

public class LandFactory implements AnimalFactory {
 public Animal createAnimal() {
 return new Elephant();
 }
}

Implementation of an Animal. This class is grouped with the first abstract factory.


package com.javapapers.sample.designpattern.abstractfactory;

public class Shark implements Animal {
 public void breathe() {
 System.out.println("I breathe in water! He he!");
 }
}

Implementation of an Animal. This class is grouped with the second abstract factory.


package com.javapapers.sample.designpattern.abstractfactory;

public class Elephant implements Animal {
 public void breathe() {
 System.out.println("I breathe with my lungs. Its easy!");
 }
}

Following class consumes the abstract factory.


package com.javapapers.sample.designpattern.abstractfactory;

public class Wonderland {
 public Wonderland(AnimalFactory factory) {
 Animal animal = factory.createAnimal();
 animal.breathe();
 }
}

Testing the abstract factory design pattern.


package com.javapapers.sample.designpattern.abstractfactory;

public class SampleAbstractFactory {

 public static void main(String args[]){
 new Wonderland(createAnimalFactory("water"));
 }

 public static AnimalFactory createAnimalFactory(String type){
 if("water".equals(type))
 return new SeaFactory();
 else
 return new LandFactory();
 }
}

Output of the above sample program for abstract factory pattern


<code>I breathe in water! He he!</code>

Advertisements