views:

64

answers:

3

I need to extend several class in my c++ application without changing any code in application. (Product extend as a solution). Is there a specific design pattern to do that?

+3  A: 

Your question is a bit vague. In order to extend an application without changing any code, the application would have to provide a specific mechanism for extensibility, e.g. a plug-in system.

Space_C0wb0y
Yes, Can you send a code sample for a such mechanism.
Janaka
@Janaka: This will not help you. You need to check if the code you have already provides such a system. It cannot be added without changing code.
Space_C0wb0y
A: 

Something like this :

http://code.google.com/p/pococapsule/

?

VJo
A: 

That depends on what can of extensibility you want. Would it be ok if the extension requires re-linking the code? If it's ok then you can use a factory method and polymorphism.

struct Extension {
    virtual ~Extension() { }
    // ...
};

Extension* load_extension()
{
    ifstream config_file(".conf");
    string line;
    getline(config_file, line);
    if( line == "this extension" ) return new ThisExtension();
    else if( line == "that extension" ) return new ThatExtension();
    // else if ...
    else return NoExtension();
}

Here, to create a new extension all you need to do is subclass from Extension and add a line to the factory method. That's re-compiling one file and relinking the project.

If it is not ok to re-link the application then you can load a dynamic library at runtime.

wilhelmtell