of(T instance) { return new ValueFactory<>(instance); } private final T value; public ValueFactory(T value) { this.value = value; } @Override public T get(Linker linker) { return value; } }
VisitHandler visitHandler = objectGraph.get(VisitHandler.class); visitHandler.visit(); } Get factory for class VisitHandler Get factory for class Counter Get factory for class Logger Get factory for class java.io.PrintStream Visits increased: 1
VisitHandler visitHandler = objectGraph.get(VisitHandler.class); visitHandler.visit(); VisitHandler visitHandler2 = objectGraph.get(VisitHandler.class); visitHandler2.visit(); } Get factory for class VisitHandler Get factory for class Counter Get factory for class Logger Get factory for class java.io.PrintStream Visits increased: 1 Get factory for class VisitHandler Get factory for class Counter Get factory for class Logger Get factory for class java.io.PrintStream Visits increased: 2
Get factory for class Counter Link factory for class Counter Get factory for class Logger Link factory for class Logger Get factory for class java.io.PrintStream Link factory for class java.io.PrintStream Visits increased: 1 Get factory for class VisitHandler Visits increased: 2 public static void main(String... args) { ObjectGraph objectGraph = buildObjectGraph(); VisitHandler visitHandler = objectGraph.get(VisitHandler.class); visitHandler.visit(); VisitHandler visitHandler2 = objectGraph.get(VisitHandler.class); visitHandler2.visit(); }
DatabaseManager databaseManager = objectGraph.get(DatabaseManager.class); databaseManager.updateVisits(3); } Exception in thread "main" java.lang.IllegalStateException: No factory for interface app.db.Database at injection.Linker.loadFactory(Linker.java:41) at injection.Linker.factoryFor(Linker.java:21) at app.db.DatabaseManager$$Factory.link(DatabaseManager$$Factory.java:17) at injection.factory.SingletonFactory.link(SingletonFactory.java:20) at injection.Linker.factoryFor(Linker.java:22) at injection.ObjectGraph.get(ObjectGraph.java:13) at Main.main(Main.java:11)