jeudi 2 juillet 2020

Which design pattern to use for usage of 2 generated versions of same class

We have web application which have also soap API (lets call it apiVersion1). Now we need to add some new functionality and due to that, we need to implement apiVersion2, but for some consumers we still need to keep alive apiVersion1. Let say we have following 2 classes generated from WSDL:

pkg.apiVersion1.SomeGeneratedClass
pkg.apiVersion2.SomeGeneratedClass

In apiVersion1 we have used something like this:

pkg.apiVersion1.SomeGeneratedClass someClass = converter.getSomeGeneratedClass(someClassVO);
someClass.setProperty(xyz);
...

Now I need to change our code as follows:

if(isapi2v0Supported){
    pkg.apiVersion2.SomeGeneratedClass someClass = converter.getSomeGeneratedClass(someClassVO);
    someClass.setProperty(xyz);
    ...
}
else{
    pkg.apiVersion1.SomeGeneratedClass someClass = converter.getSomeGeneratedClass(someClassVO);
    someClass.setProperty(xyz);
    ...
}

But I don't like that solution and I would like to use it somehow like this, so I will do not need to change usage of SomeGeneratedClass, because it is exactly same for pkg.apiVersion1.SomeGeneratedClass and pkg.apiVersion2.SomeGeneratedClass:

SomeGeneratedClassWrapper someClass = converter.getSomeGeneratedClass(someClassVO);
someClass.setProperty(xyz);
...

Is there any design pattern which I can use for this purpose? If not, do you have some advice how to deal with this specific problem? Please notice that mentioned 2 classes are generated from WSDL, so we can not (or don't want) to modify them.

Aucun commentaire:

Enregistrer un commentaire