Thursday, 15 March 2007

Abstract Factory

As one of the Gang of Four catalogued patterns, the Abstract factory Provides an interface for creating families of related or dependent objects without specifying their concrete classes.

One of the nice things about abstraction is that it lets you take care of the bigger picture and worry about the details later.

An abstract factory class defines methods to create an instance of each abstract class that represents a user interface widget. Concrete factories are concrete subclasses of an abstract factory that implements its methods to create instances of concrete widget classes for the same platform e.g a uniform frame for cars no matter what type of car,which would be applied to all cars and then the other extras taken care of later such as engine, steering and lihgts.

In a more general context, an abstract factory class and its concrete subclasses organize sets of concrete classes that work with different but related products.
An example would be a pasta maker. A pasta maker will produce different types of pasta, depending what kind of disk is loaded into the machine. All disks should have certain properties in common, so that they will work with the pasta maker. This specification for the disks is the Abstract Factory, and each specific disk is a Factory.
You will never see an Abstract Factory, because one can never exist, but all Factories (pasta maker disks) inherit their properties from the abstract Factory. In this way, all disks will work in the pasta maker, since they all comply with the same specifications. The pasta maker doesn't care what the disk is doing, nor should it. You turn the handle on the pasta maker, and the disk makes a specific shape of pasta come out. Each individual disk contains the information of how to create the pasta, and the pasta maker does not.

Applicability of this pattern would be in some of the following senarios
when;

-a system should be independent of how its products are created, composed, and represented.

-a system should be configured with one of multiple families of products.

-a family of related product objects is designed to be used together, and you need to enforce this constraint.

-you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

The Abstract Factory pattern has the following benefits and liabilities:

It isolates concrete classes. The Abstract Factory pattern helps control the classes of objects that an application creates. Because a factory encapsulates the responsibility and the process of creating product objects, it isolates clients from implementation classes. Users don`t need to know all properties of an oject or class to design it. Instances are manipulated through their abstract interfaces.

It makes exchanging product families easy. The class of a concrete factory appears only once in an application—that is, where it's created. This makes it easy to change the concrete factory an application uses. It can use different product configurations simply by changing the concrete factory. Because an abstract factory creates a complete family of products, the whole product family changes at once. Forexample,pasta maker disks inherit their properties from the abstract Factory. In this way, all disks will work in the pasta maker, since they all comply with the same specifications.

It promotes consistency among products. When product objects in a family are designed to work together, it's important that an application use objects from only one family at a time. AbstractFactory makes this easy to enforce. Same above example applies where its only the change of disks thats done and not the pasts maker.

However, extending abstract factories to produce new kinds of Products isn't easy. That's because the AbstractFactory interface fixes the set of products that can be created. Supporting new kinds of products requires extending the factory interface, which involves changing the AbstractFactory class and all of its subclasses. Forexample, the pasta maker would`nt make biscuits as its only tuned to making pasta. So each Abstract factory only applies to production of a certain product forexample cars only or pasts only.

Design Patterns,(Addison-Wesley 1998).Author GoF; Gama, HelmJohnson& Vlissides
http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/FactoryPattern.htm

1 comment:

John Ssebaale said...

Hi, thanks for the work on Abstract Factory. I learned a couple of things from this blog. I also did something on the Abstract Factory pattern, check out my blog and see whether the information is helpful. Thanks for the comprehensive study on the pattern
Cheers