Opentopia Directory Encyclopedia Tools

Abstract factory pattern

Encyclopedia : A : AB : ABS : Abstract factory pattern


A software design pattern, the Abstract Factory Pattern provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software would create a concrete implementation of the abstract factory and then use the generic interfaces to create the concrete objects that are part of the theme. The client does not know (nor care) about which concrete objects it gets from each of these internal factories since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from its general usage.

An example of this would be an abstract factory class documentCreator that provides interfaces to create a number of products (eg. createLetter() and createResume()). The system would have any number of derived concrete versions of the documentCreator class like fancyDocumentCreator or modernDocumentCreator, each with a different implementation of createLetter() and createResume() that would create a corresponding object like fancyLetter or modernResume. Each of these products is derived from a simple abstract class like Letter or Resume of which the client is aware. The client code would get an appropriate instantiation of the documentCreator and call its factory methods. Each of the resulting objects would be created from the same documentCreator implementation and would share a common theme. (They would all be fancy or modern objects.) The client would need to know how to handle only the abstract Letter or Resume class, not the specific version that it got from the concrete factory.

In software development, a Factory is the location in the code at which objects are constructed. The intent in employing the pattern is to insulate the creation of objects from their usage. This allows for new derived types to be introduced with no change to the code that uses the base object.

Use of this pattern makes it possible to interchange concrete classes without changing the code that uses them, even at runtime. However, employment of this pattern, as with similar design patterns, incurs the risk of unnecessary complexity and extra work in the initial writing of code.

How to use it

The factory determines the actual concrete type of object to be created, and it is here that the object is actually created (in C++, for instance, by the new operator). However, the factory only returns an abstract pointer (or wrapper class) to the created concrete object.

This insulates client code from object creation by having clients ask a factory object to create an object of the desired abstract type and to return an abstract pointer to the object.

As the factory only returns an abstract pointer, the client code (which requested the object from the factory) does not know - and is not burdened by - the actual concrete type of the object which was just created. In particular, this means:

And this is...

Structure

The class diagram of this design pattern is as shown below:

Examples

#include 
using std::auto_ptr;

class Control ;

class PushControl : public Control ;

class Factory ;

class ControlFactory : public Factory };

auto_ptr Factory::getFactory(int classKey) }

/*
* GUIFactory example
*/

abstract class GUIFactory else } public abstract Button createButton(); }

class WinFactory:GUIFactory }

class OSXFactory:GUIFactory }

abstract class Button

class WinButton:Button }

class OSXButton:Button }

class Application //output is //I'm a WinButton: Play //or //I'm a OSXButton: Play }

# GUIFactory example on Perl

package GUIFactory;

sub getFactory($$) else }

package GtkFactory; use base 'GUIFactory';

sub new , shift); }

sub createButton

package TkFactory; use base 'GUIFactory';

sub new , shift); }

sub createButton()

package Button;

sub new ; $self = ''; bless($self, $class); return $self; }

package GtkButton; use base 'Button';

sub paint()

package TkButton; use base 'Button';

sub paint()

package main;

my $aFactory = GUIFactory->getFactory; my $aButton = $aFactory->createButton; $aButton-> = "Play"; $aButton->paint();

See also

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: