USER'S GUIDE SOURCE CODE SAMPLES CONTACT DONATE

In a multi-module project you may have a case when a feature can to be implemented in many ways, e.g. DbManger needs a driver to work with a database. It might be sqlite, postgres or any other. DbManger does a lot - supports JPQL, mappings, DAO and other cool features. However, it depends on the driver that allows it to communicate with the exact DB implementation. Let’s see how Jeta Implementation can help:

class DbManger {
    DbDriver driver;

    public DbManger() {
        driver = MetaHelper.getImplementation(DbDriver.class);
        if(driver == null)
            throw new IllegalStateException("Db driver not defined");
    }
}

interface DbDriver {
    // methods to work with a db...
}

In a module, where we want to use DbManger, we can provide an implementation:

@Implementation(DbDriver.class)
class SqliteDbDriver implements DbDriver {
    //...
}

Furthermore, in the test module we can substitute the driver with a fake one:

@Implementation(DbDriver.class, priority = 10)
class TestDbDriver implements DbDriver {
    //...
}

MetaHelper

The helper method would be:

public static <I> getImplementation(Class<I> of) {
    return new ImplementationController<I>(metasitory, of).getImplementation();
}

In addition to getImplementation(), ImplementationController provides getImplementations() method, if it’s necessary to get all implementations, and hasImplementation() to check if there is any.

How it works:

User's Guide:

Androjeta

Advanced

Stable version: 2.3
Available on Java 1.7+
Android Compatible
Fork me on GitHub