I’m working with a third party product which has provided an API. This API works by creating an implementation of a base class, and then in the app.config indicating the implementation that you want to use.
The problem with this is that it’s possible to have multiple projects in this third party application. What I would like to do is create a wrapper class which implements the base class. This would look at the parameters and then look up a configuration to determine which other class to pass the processing over to, depending on which project is being used. This way we could add future projects to the system without modifying any of the existing code.
public class MyImplementation : ThirdPartyBaseClass
{
public override OnLoad(ThirdPartyType data)
{
//do stuff
}
public override Process(ThirdPartyType data)
{
//do stuff
}
}
There are about 15 methods that can be overridden. The base class methods appear to be empty because nothing happens if you don’t override a method, so I would need my wrapper to be able to handle the situation where the type I need to use for this project might not implement some or all of the methods.
Anybody know of a suitable design pattern for this situation?
As said by Robert in comments, Abstract Factory seems appropriate for this one.
Check this wiki link and this dofactory link for more information on this one.
For a more concrete response, I have a few doubts.
There is an object of the base class which is got from the third party API. Now, when you say that is is possible to have multiple projects in this tool, do I take it to mean that you need to use this base class to create multiple “project” classes as defined by you?
Then, the wrapper class can have an object type of an interface
IProject. This should have all the definitoins likeOnLoadandProcess. Each type of project will have a concrete class with the final implementation depending on the project type.Hope this helps in giving you a direction!