vendredi 31 juillet 2015

Call a method without a reference to it from a separate jar library

I am writing a library in Java. A object in the library is referenced by the main application.

The application requires a method to be called at a certain point on one of its objects. The library object wants to invoke this method in the application, with parameters.

I do not want to pass the application object to the library object as a reference because I want it loosely coupled and the Application object is specific to that application. If I pass it just as a Object data type, it will not have the method attached as not that type without casting.

As they are separate jars, static will not work either and want it loosely coupled again.

Example

Application A is running. The library B wants to invoke a method in a object in Application A at a certain point.

Thoughts on solution

  • Was thinking maybe somehow use the notify method on the object. But then the parameters are an issue.
  • Could a generic Object type be passed, and then the type of the object is passed as an argument? Then the object can be cast and the method called?
  • Is this possible? Does a clever design pattern exist?

Aucun commentaire:

Enregistrer un commentaire