Click here to Skip to main content
6,305,776 members and growing! (17,847 online)
Email Password   helpLost your password?
Development Lifecycle » Design and Architecture » Design and Strategy     Intermediate

Creational Pattern – Abstract Factory

By Somenath Mukhopadhyay

An article on Abstract Factory Design Pattern.
C++, Windows, Visual Studio, Dev
Posted:24 Feb 2005
Updated:14 Mar 2005
Views:35,288
Bookmarked:15 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
19 votes for this article.
Popularity: 1.97 Rating: 1.54 out of 5
14 votes, 73.7%
1
2 votes, 10.5%
2
1 vote, 5.3%
3
2 votes, 10.5%
4

5

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.

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

About the Author

Somenath Mukhopadhyay


Member

Location: United States United States

Other popular Design and Architecture articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralNeed to remove dependency on the objects from factory Pinmemberconrad Braam12:35 10 Dec '07  
Generalwrong use of pattern and typeid Pinmemberdibeas22:53 27 May '07  
GeneralWish you elaborated more Pinmemberlallous22:38 26 Nov '06  
GeneralVery good sample on client side! PinmemberFanzhe Cui4:44 16 Oct '06  
GeneralRe: Very good sample on client side! Pinmemberdibeas22:58 27 May '07  
Generalleak memory is found!!! Pinmemberyassi0:37 14 Oct '05  
GeneralUseless PinmemberMartin Holzherr22:38 24 Feb '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Mar 2005
Editor: Smitha Vijayan
Copyright 2005 by Somenath Mukhopadhyay
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project