Abstract Factory Pattern
Author: Deron Eriksson
Description: This Java tutorial describes the abstract factory pattern, a popular creational design pattern.
Tutorial created using:
Windows XP || JDK 1.6.0_10 || Eclipse JEE Ganymede SR1 (Eclipse 3.4.1)
The abstract factory pattern is a creational design pattern. An abstract factory is a factory that returns factories. Why is this layer of abstraction useful? A normal factory can be used to create sets of related objects. An abstract factory returns factories. Thus, an abstract factory is used to return factories that can be used to create sets of related objects. As an example, you could have a Ford Explorer factory that returns car part objects (mufflers, air filters, etc) associated with a Ford Explorer. You could also have a Chevy Tahoe factory that returns car part objects associated with a Chevy Tahoe. We could create an abstract factory that returns these different types of car factories depending on the car that we were interested in. We could then obtain car part objects from the car factory. Via polymorphism, we can use a common interface to get the different factories, and we could could then use a common interface to get the different car parts. Now, we'll look at the code for another simple example of the abstract factory design pattern. The sample project is shown here: Our AbstractFactory will return either a MammalFactory or a ReptileFactory via the SpeciesFactory return type. MammalFactory and ReptileFactory are subclasses of SpeciesFactory. AbstractFactory.javapackage com.cakes; public class AbstractFactory { public SpeciesFactory getSpeciesFactory(String type) { if ("mammal".equals(type)) { return new MammalFactory(); } else { return new ReptileFactory(); } } } SpeciesFactory is an abstract class with the getAnimal() abstract method. This method returns an Animal object. The polymorphism of AbstractFactory is achieved because its getSpeciesFactory() method returns a SpeciesFactory, regardless of the actual underlying class. This polymorphism could also be achieved via an interface rather than an abstract class. SpeciesFactory.javapackage com.cakes; import com.cakes.animals.Animal; public abstract class SpeciesFactory { public abstract Animal getAnimal(String type); } MammalFactory implements getAnimal(). It returns an Animal, which is either a Dog or a Cat. MammalFactory.javapackage com.cakes; import com.cakes.animals.Animal; import com.cakes.animals.Cat; import com.cakes.animals.Dog; public class MammalFactory extends SpeciesFactory { @Override public Animal getAnimal(String type) { if ("dog".equals(type)) { return new Dog(); } else { return new Cat(); } } } ReptileFactory implements getAnimal(). It returns an Animal, which is either a Snake or a Tyrannosaurus. ReptileFactory.javapackage com.cakes; import com.cakes.animals.Animal; import com.cakes.animals.Snake; import com.cakes.animals.Tyrannosaurus; public class ReptileFactory extends SpeciesFactory { @Override public Animal getAnimal(String type) { if ("snake".equals(type)) { return new Snake(); } else { return new Tyrannosaurus(); } } } (Continued on page 2) Related Tutorials:
|