From 1d62c868aab46d8be13f2f81f722f09bf1b93981 Mon Sep 17 00:00:00 2001 From: Kai Nagel Date: Mon, 28 Oct 2024 14:17:02 +0100 Subject: [PATCH] ConstructorInjection --- .../RunReflectiveConfigGroupExample.java | 2 +- .../ConstructorInjection.java | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/matsim/codeexamples/guicewithoutmatsim/ConstructorInjection.java diff --git a/src/main/java/org/matsim/codeexamples/config/reflectiveConfigGroup/RunReflectiveConfigGroupExample.java b/src/main/java/org/matsim/codeexamples/config/reflectiveConfigGroup/RunReflectiveConfigGroupExample.java index 824664c6..87115f3c 100644 --- a/src/main/java/org/matsim/codeexamples/config/reflectiveConfigGroup/RunReflectiveConfigGroupExample.java +++ b/src/main/java/org/matsim/codeexamples/config/reflectiveConfigGroup/RunReflectiveConfigGroupExample.java @@ -45,7 +45,7 @@ public static void main(String[] args) { config.checkConsistency(); - ControlerUtils.checkConfigConsistencyAndWriteToLog(config, "test"); + ControlerUtils.checkConfigConsistencyAndWriteToLog(config, "test" ); } diff --git a/src/main/java/org/matsim/codeexamples/guicewithoutmatsim/ConstructorInjection.java b/src/main/java/org/matsim/codeexamples/guicewithoutmatsim/ConstructorInjection.java new file mode 100644 index 00000000..43e20730 --- /dev/null +++ b/src/main/java/org/matsim/codeexamples/guicewithoutmatsim/ConstructorInjection.java @@ -0,0 +1,58 @@ +package org.matsim.codeexamples.guicewithoutmatsim; + +import com.google.inject.Inject; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +class ConstructorInjection{ + + private static final Logger log = LogManager.getLogger( org.matsim.codeexamples.guicewithoutmatsim.PartOne.class ) ; + + public static void main(String[] args){ + Helper helper = new MyHelper1(); + Simulation simulation = new MySimulation1( helper ); + simulation.run(); + } + + interface Simulation { + void run() ; + } + + interface Helper { + Object getAccessToSomething() ; + } + + static class MySimulation1 implements Simulation{ + private final Helper helper; + MySimulation1( Helper helper ) { + this.helper = helper; + } + @Override public void run() { + log.info( "called MySimulation1 run method") ; + helper.getAccessToSomething() ; + } + } + static class MySimulation2 implements Simulation{ + private final Helper helper; + MySimulation2( Helper helper ) { + this.helper = helper; + } + @Override public void run() { + log.info( "called MySimulation2 run method") ; + helper.getAccessToSomething() ; + } + } + + static class MyHelper1 implements Helper{ + @Override public Object getAccessToSomething(){ + log.info( "called MyHelper1 getAccess... method") ; + return null ; + } + } + static class MyHelper2 implements Helper{ + @Override public Object getAccessToSomething(){ + log.info( "called MyHelper2 getAccess... method") ; + return null ; + } + } +}