views:

94

answers:

2

I'm working on API which should provide simple access to number of remote web-service based resources.

Some of these remote resources requires special parameters to be passed before interaction. For example, one of them requires pair of developer's keys to be passed, another requires pair of keys and unique identifier. Third one doesn't require these parameters at all. I'm working with 3 services now but their number can be enlarged.

For each web-service I have correspondent implementation of my API. The problem is that I don't know how to introduce to my API possibility to pass unknown number of Strings with unknown meanings.

Some of my suggestions:

1.

ServiceFactory.createService (ServiceEnum type, Properties keys);

2.

ServiceFactory.createService (ServiceEnum type, ServiceParams params);

Where ServiceParams is a marker-interface. In this case I'll have some helper-class like this:

public class ServiceHelper {

   public static ServiceParams createFirstServiceParams (String secretKey, String publicKey);

   public static ServiceParams createSecondServiceParams (String secretKey, String publicKey, String uid);

   public static ServiceParams createThirdServiceParams ();
} 

Pros: meaningful parameter names for each service.

Cons: if I provide support for fourth service then user will have to update factories module. In the first case user will only have to download new module.

3.

ServiceFactory.createService (ServiceEnum type, String ... params);

Pros: the most easy to use. User don't need to do any additional actions (like creating properties of ServiceParams).

Cons: the most unobvious way. User should know which set of params corresponds to the service he wants to create.

4-6:

the same variants but params are being passed not to factory method but to Service instance (in its init() method for example).

Pros: user can change keys for his service if he need without necessary to create new instance of the same service.

Cons: more complicated way, profit is questionable.

Which variant do you prefer? Why? Your variants are welcome.

+3  A: 

You could have two factory methods, one where you pass a Map containing parameters, and the other without parameters:

ServiceFactory.createService(ServiceEnum type);
ServiceFactory.createService(ServiceEnum type, Map<String,?> params);

In this case, it's the responsibility of the caller to get the parameters right, but it gives you maximal flexibility.

uckelman
+1  A: 

I would probably go with option 1 and replace Properties with Map, which is what Properties uses for its underlying implementation.

James McMahon