Is the Factory Pattern suitable for this?
Hi guys,
I have the following question: I have some functionality which is divided in two parts: basic functionality that is created implementing only one interface , i.e. IBasicClient, and functionality that is methods with different data types as parameters , i.e.:
ISDOClient, IStAXClient , which both have:
ISDOClient:
==============
public void fireAndForget(QName opName, DataObject payload);
public void sendReceive(QName opName, DataObject payload, DataObject result);
//... and so on
IStAXClient:
==============
public void fireAndForget(QName opName, XMLStreamReader payload);
public void sendReceive(QName opName, XMLStreamReader payload, XMLStreamWriter writer);
//... and so on
-
each of these interfaces (IStAXClient, ISDOClient, etc) extends the interface IBasicClient, which contains methods like:
public void setup();
public void engageModule();
and so on ....
the implementations of these look like that :
BasicClientImpl implements IBasicClient
SDOClientImpl extends BasicClientImpl implements ISDOClient
StAXClientImpl extends BasicClientImpl implements IStAXClient
and so on ...
My question is: How to make a factory that will most suitably instantiate these ?
There are several options which I can think of :
1) create a class ClientFactory that will have methods :
public Object newClient(String clientClassName, String arg1);
public Object newClient(String clientClassName, String arg1, String arg2);
where arg1, arg2, etc. are the parameters that constructors take. The Factory will use reflection internally, but the problem which that approach is that a developer has to explicitly cast the client to the approproate type before using it. I cannot return a "IBasicClient" type object, because IBasicClient does not contain the actual execution methods of my functionality, such as : sendReceive(...) , fireAndForget(..) and so on, so casting will also be necessary.
2) do it like that :
public ISDOClient newSDOClient(String arg1);
public ISDOClient newSDOClient(String arg1, String arg2);
....
public IStAXClient newStAXClient(String arg1);
public IStAXClient newStAXClient(String arg1, String arg2);
... etc
which is more type safe, but whenever a new "type" of client is added, the factory's public methods have to be changed, i.e. I have to add creational methods for the new type of client.
Which method 1) or 2) would you suggest, or , alternatively, is there a better way using Java to achieve what I want? Thank you very much in advance.
Regards,
Angel

