Click here to Skip to main content
15,895,606 members
Articles / Programming Languages / C++
Article

Creational Pattern – Abstract Factory

Rate me:
Please Sign up or sign in to vote.
1.40/5 (19 votes)
14 Mar 20052 min read 65.8K   670   21   8
An article on Abstract Factory Design Pattern.

Introduction

The design pattern can be described as solving some piece of problem in a wise manner. We can solve an entire class of similar problems using a specific design pattern. There are three basic categories of design patterns, namely: Creational, Structural and Behavioral.

In this article, I have tried to explain one of the important creational design patterns, i.e., Abstract Factory Pattern, with a simple example. Creational pattern deals with how an object can be created. This means that the object creation process will be isolated from the details of its creation, and as a result, we don’t have to change the code whenever a new type of product is to be created.

Abstract Factory method “provides an interface for creating families of related or dependent objects without specifying their concrete classes”. Abstract Factory pattern can be used when a system should be independent of how its products are created.

The main participants of the Abstract Factory are:

  1. Abstract Factory: In the example, the class CShapeFactory is the abstract factory. This class just exposes an interface to be realized in the concrete factory classes.
  2. Concrete Factory: Implements the operations to create concrete product objects. In the example, CNormalShapeFactory and CEnhancedShapeFactory are working as two concrete factories.
  3. Product: Here, CMyLine and CMyTriangle, CMyDashedLine and CMyFilledTriangle are the classes to produce these products.
  4. Client: Uses the interface exposed by the Abstract Factory class. Here, canvass is working as the client class.

Image 1

Fig: Class diagram of the example code

The concrete factory classes are usually implemented as Singleton. If we look at the example, we will find how CNormalShapefactory and CEnhancedShapefactory have been implemented as Singletons. Look at the following implementation of the CNormalShapefactory ::Instance function. It returns a pointer to the one and only one instance. And the client code always accesses this instance through CNormalShapeFactory ::Instance() function. The same thing is happening for the CEnhancedShapeFactory class.

static CNormalShapeFactory* Instance()
{

    if(NormalShapeFactoryInstance == 0)
        NormalShapeFactoryInstance = new CNormalShapeFactory;

    return NormalShapeFactoryInstance;
}                              

private:
    static NormalShapeFactory* NormalShapeFactoryInstance;

In the example, the first concrete class CNormalShapeFactory is creating a picture made up with solid lines and a triangle made up with those solid lines; whereas the second concrete class CEnhancedShapeFactory is creating a picture with dashed lines and a filled triangle.

Note: To run the example in MSVC environment, please enable the RTTI option in the project settings.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
LearningDesires3-Jun-10 19:57
LearningDesires3-Jun-10 19:57 
GeneralNeed to remove dependency on the objects from factory Pin
conrad Braam10-Dec-07 11:35
conrad Braam10-Dec-07 11:35 
Generalwrong use of pattern and typeid Pin
dibeas27-May-07 21:53
dibeas27-May-07 21:53 
GeneralWish you elaborated more Pin
Elias Bachaalany26-Nov-06 21:38
Elias Bachaalany26-Nov-06 21:38 
GeneralVery good sample on client side! Pin
Fanzhe Cui16-Oct-06 3:44
Fanzhe Cui16-Oct-06 3:44 
GeneralRe: Very good sample on client side! Pin
dibeas27-May-07 21:58
dibeas27-May-07 21:58 
Generalleak memory is found!!! Pin
yassi13-Oct-05 23:37
yassi13-Oct-05 23:37 
GeneralUseless Pin
Martin Holzherr24-Feb-05 21:38
Martin Holzherr24-Feb-05 21:38 
Sleepy | :zzz: The article does not provide anything more than one of the numerous books and articles about this subject.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.