Encyclopedia > Abstract factory pattern

  Article Content

Abstract factory pattern

A software design pattern, the abstract factory pattern provides a way to create an object obeying the given interface without specifying concrete implementation classes. 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 specific concrete classes from object creation.

Use of the pattern makes it possible to interchange concrete class without changing the code that use 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.

The pattern is also used for to support other patterns such as the builder pattern.

The ClassFactory[?] class is an implementation of the factory in the Java standard library[?].

See also

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, via 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:

  • The client code has no knowledge whatsoever of the concrete type, not needing to include any headers files or class declarations relating to the concrete type. The client code deals only with the abstract type. Objects of a concrete type are indeed created by the factory, but the client code accesses such objects only through their abstract interface.

  • Adding new concrete types is done by modifying the client code to use a different factory, a modification which is typically one line in one file. (The different factory then creates objects of a different concrete type, but still returns a pointer of the same abstract type as before - thus insulating the client code from change.) This is significantly easier than modifying the client code to instantiate a new type, which would require changing every location in the code where a new object is created (as well as making sure that all such code locations also have knowledge of the new concrete type, by including for instance a concrete class header file). If all factory objects are stored globally in a singleton object, and all client code goes through the singleton to access the proper factory for object creation, then changing factories is as easy as changing the singleton object.

Example

 /*
 * GUIFactory example in C#
 */

 abstract class GUIFactory {

     public static GUIFactory getFactory() {
         int sys = readFromConfigFile("OS_TYPE");
         if (sys==0) {
             return(new WinFactory());
         } else {
             return(new OSXFactory());
         }
    }
    public abstract Button createButton();
 }

 class WinFactory:GUIFactory {
     public override Button createButton() {
         return(new WinButton());
     }
 }

 class OSXFactory:GUIFactory {
     public override Button createButton() {
         return(new OSXButton());
     }
 }

 abstract class Button  {
     public string caption;
     public abstract void paint();
 }

 class WinButton:Button {
     public override void paint() {
        Console.WriteLine("I'm a WinButton: "+caption);
     }
 }

 class OSXButton:Button {
     public override void paint() {
        Console.WriteLine("I'm a OSXButton: "+caption);
     }
 }

 class Application {
     static void Main(string[] args) {
         GUIFactory aFactory = GUIFactory.getFactory();
         Button aButton = aFactory.createButton();
         aButton.caption = "Play";
         aButton.paint();
     }
     //output is
     //I'm a WinButton: Play
     //or
     //I'm a OSXButton: Play
 }



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Explorer

... Ingstad, (1899-2001), Norwegian, Danish explorer, Governor of Greenland J Louis Joliet, (1645-1700) Richard Jobson L Jean François La Pérouse, (1741-1788), ...

 
 
 
This page was created in 22.3 ms