Click here to Skip to main content
11,934,809 members (50,786 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

Factory class with Open Closed Principle

, 30 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple Factory class with Open Closed principle.


This is to demonstrate a simple template based factory class which can be used as a generic class satisfying the concept of Open Closed Principle (OCP). There could be many enhancements but to get the idea straight, I am keeping it simple. Nothing much to write, the code speaks itself.

Using the code

The BaseFactory class is the main class to demonstrate the above idea. A specialized type of factory instance for some Product type hierarchy can be created as below:

BaseFactory<Product> productFactory;

To register a new Product type:


To create instance of a Product type:

Product* product = productFactory.CreateInstance("P1");

Below is the complete implementation for the BaseFactory class:

// Base template Factory class

template<class T>
class BaseFactory
    T *CreateInstance(std::string typeName)
        TypeMap::iterator it = mTypeMap.find(typeName); 

        if ( it != mTypeMap.end())
            return it->second();
        return NULL;
    template<class DerivedT> 
    void RegisterType(std::string derivedTypeName)
        this->mTypeMap[derivedTypeName] = &BaseFactory<T>::CreateT<DerivedT>;

    typedef std::map<std::string, T *(*)()> TypeMap;
    TypeMap mTypeMap;
    template<class DerivedT>
    static T* CreateT() { return new DerivedT; }


  • 30 Apr 2013: Initial revision.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manish K. Agarwal
Software Developer (Senior) Oracle
India India
Working with Oracle. Using C/C++, VC++, MFC, STL, C#, Java etc. on various platform like Windows, Unix, Macintosh etc. from last 13+ years to convert various type of requirements into running software components. My core expertise is multithreaded desktop product and large scale enterprises software development.

You may also be interested in...

Comments and Discussions

QuestionI’m a bit confused. Pin
George Swan30-Apr-13 21:19
memberGeorge Swan30-Apr-13 21:19 

Thanks for the interesting example. I thought that the idea behind the Factory Method Pattern was to hide concrete classes from the client? In your example, the client has to know about the concrete types in order to register them with the Factory. So, it seems to me , the Factory is not fully encapsulating the concrete types.

AnswerRe: I’m a bit confused. Pin
Manish K. Agarwal1-May-13 2:45
memberManish K. Agarwal1-May-13 2:45 
QuestionVery Nice Pin
John Bandela30-Apr-13 5:51
memberJohn Bandela30-Apr-13 5:51 
AnswerRe: Very Nice Pin
Manish K. Agarwal30-Apr-13 7:39
memberManish K. Agarwal30-Apr-13 7:39 
GeneralMy vote of 5 Pin
jsolutions_uk30-Apr-13 1:46
groupjsolutions_uk30-Apr-13 1:46 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 30 Apr 2013
Article Copyright 2013 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid