Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / C++
Tip/Trick

Factory class with Open Closed Principle

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
30 Apr 2013CPOL 16.3K   108   4   5
Simple Factory class with Open Closed principle.

Introduction

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:

C++
BaseFactory<Product> productFactory;

To register a new Product type:

C++
productFactory.RegisterType<P1>("P1");

To create instance of a Product type:

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

Below is the complete implementation for the BaseFactory class:

C++
// Base template Factory class

template<class T>
class BaseFactory
{
public:
    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>;
    }

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

History

  • 30 Apr 2013: Initial revision.

License

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


Written By
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.

Comments and Discussions

 
QuestionI’m a bit confused. Pin
George Swan30-Apr-13 20:19
mveGeorge Swan30-Apr-13 20:19 
AnswerRe: I’m a bit confused. Pin
Manish K. Agarwal1-May-13 1:45
Manish K. Agarwal1-May-13 1:45 
QuestionVery Nice Pin
John Bandela30-Apr-13 4:51
John Bandela30-Apr-13 4:51 
Looks very elegant. Only thing I would do differently is use std::shared_ptr as the return type.
AnswerRe: Very Nice Pin
Manish K. Agarwal30-Apr-13 6:39
Manish K. Agarwal30-Apr-13 6:39 
GeneralMy vote of 5 Pin
jsolutions_uk30-Apr-13 0:46
jsolutions_uk30-Apr-13 0: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.