0

I have n jars with same functionality but different package name ( since different regions). I have same operation performed using this n jars.

Now I want to load the corresponding region of jars, for my operation. What is the best design pattern to use in this case.

I tried with reflection by changing the package name and loading dynamically. But is there any best pattern available for this case ?

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58

2 Answers2

0

Since your different implementations have different package name it's not so important that they are in different jars. Just use the factory pattern.

Pino
  • 7,468
  • 6
  • 50
  • 69
  • I have same implementation with different package. So to keep the implementation constant as of now using reflection to load different package based on user input. But I want to do without using reflection since its very slow. – user1894220 Jun 16 '14 at 10:06
0

Will you be distributing them separately? If not you don't need to dynamically load the jars or reflect them.

Your app can reference all jars. Here is the dependencies you could have:

|Application Jar                   |
   ↓          ↓            ↓
   ↓   |Region 1 Jar| |Region 2 Jar|
   ↓          ↓            ↓
|Core Jar                          |

Core Jar is region agnostic but has one or more interfaces for what it requires from a region. Region jars have classes that implement those core interfaces and the Application jar is responsible for constructing the right region classes and plugging it all together.

If the Regions are outside of your control, and they can't depend on Core or implement it's interfaces you can use adapters:

|Application Jar                                   |
   ↓              ↓                  ↓
   ↓   |Region 1 Jar Adapter| |Region 2 Jar Adapter|
   ↓    ↓         ↓                  ↓
|Core Jar |       ↓                  ↓
             |Region 1 Jar|   |Region 2 Jar|

Here Region 1 and Region 2 are anything you like, they have no dependencies. Only one package has a dependency on them, and that is it's adapter package. The Adapter supports a core interface and knows how to translate the calls to the underlying Region Jar. The application is identical to the previous situation.

weston
  • 54,145
  • 21
  • 145
  • 203
  • I won't be able to edit the region jars. so even if i define a interface, i won't be able to declare a field to a specific type(Since each jar has same class with different package name). – user1894220 Jun 16 '14 at 10:02
  • Added alternative for when you can't edit the region jars. – weston Jun 16 '14 at 10:25
  • I agree with the design you gave, can you please explain how to translate call to underlying region jar. That will clear my question. – user1894220 Jun 16 '14 at 10:41
  • I'd read up on the [Adapter Pattern](http://en.wikipedia.org/wiki/Adapter_pattern) – weston Jun 16 '14 at 10:53