2

I'm not sure how to ask this so I will explain the problem first:

I'm developing a plugin for another software, it depends on an API not managed by me.

When the code of that API changes I need to update my plugin to make it compatible again (for example because of deprecated code, removed classes etc).

The problem is that if I change my plugin to make it compatible with the new software version, it is not compatible anymore with previous versions of the software.

Is possible to have only one plugin compatible with both versions of the software (suppose a class from the API changed the name, or was removed)? or the only solution is to maintain two versions?

Enrique
  • 4,693
  • 5
  • 51
  • 71
  • Does this API include a call which returns the version? If so, just branch your code depending on the version returned. For example, Android versions are assigned an ordered, constant value, so if ANDROID_VERSION < MARSHMALLOW use old logic, else use new logic. – MarsAtomic Nov 29 '19 at 04:40
  • Yes I can now the software version from my code, I could write an "if version" in my code, but I don't know how to compile only one plugin with both versions because they need different classes, import is failing – Enrique Nov 29 '19 at 10:14

2 Answers2

3

In some instance you will need to have two versions of code.
But you can mitigate this problem by using a Adapter like design pattern.

enter image description here

By this way, you can create a common interface that the two versions respond, and implement only the versions variations (adapt a class remove, etc).

You can search for more info in this book: https://iansommerville.com/software-engineering-book/

With this approach you can update the plugin and load the code at runtime. This answer shows how to do it: How should I load Jars dynamically at runtime?

Guilherme
  • 456
  • 4
  • 12
  • So we need to use Reflection? I mean suppose API version 1.0 is using class A, and version 2.0 replaced class A with class B. My plugin needs to extend class A if it's installed in 1.0 and B if it's installed in 2.0, is possible to manage that somehow? – Enrique Nov 29 '19 at 10:23
  • Both class A and B will need to implement the plugin interface that you defined. When you load the plugin jar at runtime, you will need to access it by the interface. Whit the interface you will not need reflection. The jar loading loads the plugin class as a Object. With this architecture you only need to update the plugin jar, not the main program. So you can ship the plugin version that the main code needs. – Guilherme Nov 29 '19 at 13:08
0

Not sure if I would be completely answering your question. When you update your code to the new version, if the your existing code base is completely changing I presume the only way forward would be to maintain 2 separate version.

If not, I would suggest that you try to re-use a lot of code across the versions by basically having checks in your code for the version being used. This could be in the form of various design patterns or could be even as simple as if/else statements; depends on the complexity of your plugin/code base. But do make sure you have tests that verify that all supported versions are working as expected. If you do not have test cases, you should first create them to cover any code you are changing.

Vinnie
  • 452
  • 5
  • 24