Interface in one project and Implementation in annother [message #893608] |
Wed, 04 July 2012 20:18 |
Ely Schoenfeld Messages: 4 Registered: June 2010 |
Junior Member |
|
|
Hello All.
(Sorry for such a long post)
I hope this hasn't been discussed recently. I couldn't find anything about it.
I'm sure I'm making some dumb error (I hope) but I've been trying for days now and can't get it working. I'm kind of new with java.
I have a simple standalone java application that needs to execute some Web Service methods provided by different companies. I thought I would define an Interface in my main application, and create separate ".jar" files with different implementations. This way, if I want to pay "Company A" services, I would implement those interfaces in a way, for sure different as those needed if I want to pay "Company B" services.
I hope this explanation is clear.
I'm not sure how to do it. I would like to put the main application and a "connection" jar in the classpath, in order to use the application. Then simply change the "connection" jar if I need to.
The problems I can see are:
a) The main class needs some "implementation" class, in order to be able to use the connection methods and process results.
b) The connection class needs to know the interface class.
Both things at the same time makes difficult to use separate projects. Right?
What I've tried is:
In the main project, I created an Interface Class like this:
package com.my.app;
public interface IApi {
String[] ExecuteOne(String pUsername, String pPassword, String pData);
}
Then, I created a new java Project and created a class in another package:
package com.my.appImp;
public class ApiImplementation implements IApi {
public String[] ExecuteOne(String pUsername, String pPassword, String pData) {
String[] result = ...
return result;
}
...
}
Of course eclipse complains with a message: "IApi cannot be resolved to a type"
So, I added the main project to the buld path in the "Projects" tab of the connection project.
Ok.
My problem now is that I can't create a new instance of "ApiImplementation" in the main project, since it is not known. If I add the connection project to the build path of the main project, it complains about "A cycle was detected in the build path of project..."
How could I solve this issue?
Must I create a Jar from the connection project and add it to the classpath of the main project in order to keep on coding? (Could it be created win an eclipse export "runnable Jar"?) And repeat this every time I change something in the connection project?
How would I be able to debug?
Thank you for your comments about this. I sure still have many things to learn .
Ely.
|
|
|
Re: Interface in one project and Implementation in annother [message #893616 is a reply to message #893608] |
Wed, 04 July 2012 22:41 |
Stephan Herrmann Messages: 1853 Registered: July 2009 |
Senior Member |
|
|
Well, this looks more like a general issue of object-oriented design than a problem with the JDT, doesn't it?
That said, I see two canonical solutions:
Either create just one more project:
- App: defines IApi and uses a reference to an IApi to invoke the service
- Provider1 - ProviderN: import IApi from App and implement the interface using the service of CompanyX
- Assembly: import one or more providers, instantiate the provider, instantiate the app and pass a reference of the selected provider into the App
Instead of cyclic dependencies you have a clean DAG.
Or, second, use reflection to instantiate a provider whose name is passed as a configuration argument (the pattern of jdbc and many more).
HTH,
Stephan
PS: I know that others will recommend dependency injection, but the above solutions are what you get even without adding more infra structure to your projects.
|
|
|
Powered by
FUDForum. Page generated in 0.02255 seconds