Call Me Back

IMAG0191

I’ve just sketched out the logic of a basic callback mechanism, allowing “code injection” from your main project into a project or .jar file included in its build path – bypassing the undesirable prospect of a circular dependency.

If my writing is too illegible in that image, the basic premise is this:

“LibraryProject” defines an interface “MyCallback”, which declares the method “callback()”. A concrete class within LibraryProject (to give it a name, “LibTestClass”) includes a public “setMyCallback(…)” method which allows a MyCallback object to be passed in.

Within “MainProject”, a class “ExampleCallback” is written, which implements MyCallback from LibraryProject.

Another concrete class within MainProject (“MainTestClass”) will instantiate an ExampleCallback object, and pass it into a LibTestClass instance via the setMyCallback(…) method.

This part is key: The overridden “callback()” method inside ExampleCallback is the code that is eventually injected into (and executed from) LibraryProject when code inside the LibTestClass instance calls {MyCallback}.callback().

That about sums it up I think.

 

TRP

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s