I'm using a library which have an interface for a certain functionality, and a few implementation of the functionality. The library also provides a Factory object that instantiates the correct functionality's implementation.
I need to extend a functionality in question by adding new methods to the object, and I wanted to do this by creating a new interface which inherits from the library's interface and roll my own implementations.
For example :
class IFromLibrary
{
virtual void LibraryMethod(void) = 0;
}
class IMyInterface : public IFromLibrary
{
virtual void MyMethod(void) = 0;
int SomeValue;
}
class TMyImplementation : public IMyInterface
{
void LibraryMethod(void) { ... }
void MyMethod(void) { ... }
}
The problem I'm facing is with the creation of an instance of TMyImplementation. Like I said, the library hides the constructors of the implementations as private members and uses Factory static methods to construct the objects.
Example :
static IFromLibrary * createFromLibrary(int whatever) { ... }
Since it's the paradigm of the library, I'm trying to do it as well by creating my own create in my implementation class :
static IMyInterface * createMyImplementation(int whatever) { ... }
The problem I have is that I'd wish that my newly instantiated object would be constructed using the values provided by the library's Factory :
static IMyInterface * createMyImplementation(int whatever)
{
IMyInterface * newObject = createFromLibrary(whatever); // this doesn't compile, evidently.
newObject->SomeValue = SomeOtherValue; // init the parts of the object that belong to my interface
}
I'm trying to avoid the adapter pattern (i.e. wrapping a IFromLibrary pointer inside my TMyImplementation class and forward all the method calls that are inherited from IFromLibrary). That would indeed work, but from my point of view, I find it strange, architecturally speaking, that I simultaneously inherit from and wrap a class. I also want to avoid all that forwarding boiler plate code, if at all possible.
Can this be done in any way?
EDIT :
- Added the fact that the private constructors are for the implementations only (sorry @NirFriedman for the confusion).
- Specified why I want (if possible) to avoid the Adapter pattern (thanks @immibis for the pattern name).