Abstract factory pattern explained

The abstract factory pattern in software engineering is a design pattern that provides a way to create families of related objects without imposing their concrete classes, by encapsulating a group of individual factories that have a common theme without specifying their concrete classes.[1] According to this pattern, a client software component creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the family. The client does not know which concrete objects it receives from each of these internal factories, as it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface.[2]

Use of this pattern enables interchangeable concrete implementations without changing the code that uses them, even at runtime. However, employment of this pattern, as with similar design patterns, may result in unnecessary complexity and extra work in the initial writing of code. Additionally, higher levels of separation and abstraction can result in systems that are more difficult to debug and maintain.

Overview

The abstract factory design pattern is one of the 23 patterns described in the 1994 Design Patterns book. It may be used to solve problems such as:[3]

Creating objects directly within the class that requires the objects is inflexible. Doing so commits the class to particular objects and makes it impossible to change the instantiation later without changing the class. It prevents the class from being reusable if other objects are required, and it makes the class difficult to test because real objects cannot be replaced with mock objects.

A factory is the location of a concrete class in the code at which objects are constructed. Implementation of the pattern intends to insulate the creation of objects from their usage and to create families of related objects without depending on their concrete classes. This allows for new derived types to be introduced with no change to the code that uses the base class.

The pattern describes how to solve such problems:

This makes a class independent of how its objects are created. A class may be configured with a factory object, which it uses to create objects, and the factory object can be exchanged at runtime.

Definition

Design Patterns describes the abstract factory pattern as "an interface for creating families of related or dependent objects without specifying their concrete classes."[4]

Usage

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

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

An example is an abstract factory class DocumentCreator that provides interfaces to create a number of products (e.g., createLetter and createResume). The system would have any number of derived concrete versions of the DocumentCreator class such asFancyDocumentCreator or ModernDocumentCreator, each with a different implementation of createLetter and createResume that would create corresponding objects such asFancyLetter or ModernResume. Each of these products is derived from a simple abstract class such asLetter or Resume of which the client is aware. The client code would acquire an appropriate instance 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. The client would only need to know how to handle the abstract Letter or Resume class, not the specific version that was created by the concrete factory.

As the factory only returns a reference or a pointer to an abstract type, the client code that requested the object from the factory is not aware of - and is not burdened by - the actual concrete type of the object that was created. However, the abstract factory knows the type of a concrete object (and hence a concrete factory). For instance, the factory may read the object's type from a configuration file. The client has no need to specify the type, as the type has already been specified in the configuration file. In particular, this means:

Structure

UML diagram

In the above UML class diagram, the Client class that requires ProductA and ProductB objects does not instantiate the ProductA1 and ProductB1 classes directly. Instead, the Client refers to the AbstractFactory interface for creating objects, which makes the Client independent of how the objects are created (which concrete classes are instantiated). The Factory1 class implements the AbstractFactory interface by instantiating the ProductA1 and ProductB1 classes.

The UML sequence diagram shows the runtime interactions. The Client object calls createProductA on the Factory1 object, which creates and returns a ProductA1 object. Thereafter, the Client calls createProductB on Factory1, which creates and returns a ProductB1 object.

Variants

The original structure of the abstract factory pattern, as defined in 1994 in Design Patterns, is based on abstract classes for the abstract factory and the abstract products to be created. The concrete factories and products are classes that specialize the abstract classes using inheritance.

A more recent structure of the pattern is based on interfaces that define the abstract factory and the abstract products to be created. This design uses native support for interfaces or protocols in mainstream programming languages to avoid inheritance. In this case, the concrete factories and products are classes that realize the interface by implementing it.

Example

This C++11 implementation is based on the pre C++98 implementation in the book.

  1. include

enum Direction ;

class MapSite ;

class Room : public MapSite ;

class Wall : public MapSite ;

class Door : public MapSite ;

class Maze ;

class MazeFactory ;

// If createMaze is passed an object as a parameter to use to create rooms, walls, and doors, then you can change the classes of rooms, walls, and doors by passing a different parameter. This is an example of the Abstract Factory (99) pattern.

class MazeGame ;

int main

The program output is:

Maze::addRoom 0x1317ed0Maze::addRoom 0x1317ef0Room::setSide 0 0x1318340Room::setSide 2 0x1317f10Room::setSide 1 0x1318360Room::setSide 3 0x1318380Room::setSide 0 0x13183a0Room::setSide 2 0x13183c0Room::setSide 1 0x13183e0Room::setSide 3 0x1317f10

See also

External links

Notes and References

  1. Book: Freeman . Eric . Robson . Elisabeth . Sierra . Kathy . Bates . Bert . Hendrickson . Mike . Loukides . Mike . 2004 . Head First Design Patterns . 1 . 156 . O'REILLY . paperback . 978-0-596-00712-6 . 2012-09-12 .
  2. Book: Freeman . Eric . Robson . Elisabeth . Sierra . Kathy . Bates . Bert . Hendrickson . Mike . Loukides . Mike . 2004 . Head First Design Patterns . 1 . 162 . O'REILLY . paperback . 978-0-596-00712-6 . 2012-09-12 .
  3. Web site: The Abstract Factory design pattern - Problem, Solution, and Applicability. w3sDesign.com. 2017-08-11.
  4. Web site: Design Patterns: Abstract Factory . Erich . Gamma . Richard Helm . Ralph Johnson . John M. Vlissides . 2009-10-23 . informIT . https://web.archive.org/web/20120516213805/http://www.informit.com/ . 2012-05-16 . 2012-05-16 . Object Creational: Abstract Factory: Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. . bot: unknown .
  5. Web site: Object Design for the Perplexed . David . Veeneman . 2009-10-23 . The Code Project . https://web.archive.org/web/20110221224616/http://www.codeproject.com/ . 2011-02-21 . 2012-05-16 . The factory insulates the client from changes to the product or how it is created, and it can provide this insulation across objects derived from very different abstract interfaces. . bot: unknown .
  6. Web site: Abstract Factory: Implementation . OODesign.com . 2012-05-16.