From b767017ef73c38d9cba3cc9d26d5ebee7ebff43d Mon Sep 17 00:00:00 2001 From: Julian Dehne <julian.dehne@uni-potsdam.de> Date: Wed, 25 Apr 2018 13:43:50 +0200 Subject: [PATCH] added Interfaces to munchkin example --- gemeinsamforschen/pom.xml | 12 ++++++++++++ .../java/unipotsdam/gf/munchkin/IMunschkin.java | 14 ++++++++++++++ .../controller/MunchkinImpl.java} | 17 ++++++++++++----- .../gf/{ => munchkin}/model/Munschkin.java | 2 +- .../{ => munchkin}/service/RestyMunschkin.java | 15 ++++++++------- .../src/test/java/MunschkinLoaderTest.java | 12 ++++++++---- 6 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/IMunschkin.java rename gemeinsamforschen/src/main/java/unipotsdam/gf/{controller/MunschkinLoader.java => munchkin/controller/MunchkinImpl.java} (84%) rename gemeinsamforschen/src/main/java/unipotsdam/gf/{ => munchkin}/model/Munschkin.java (98%) rename gemeinsamforschen/src/main/java/unipotsdam/gf/{ => munchkin}/service/RestyMunschkin.java (64%) diff --git a/gemeinsamforschen/pom.xml b/gemeinsamforschen/pom.xml index 34de8955..1fa9a005 100644 --- a/gemeinsamforschen/pom.xml +++ b/gemeinsamforschen/pom.xml @@ -8,6 +8,18 @@ <artifactId>gemeinsamforschen</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.6</source> + <target>1.6</target> + </configuration> + </plugin> + </plugins> + </build> <properties> <org.springframework.version>3.2.4.RELEASE</org.springframework.version> diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/IMunschkin.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/IMunschkin.java new file mode 100644 index 00000000..05776056 --- /dev/null +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/IMunschkin.java @@ -0,0 +1,14 @@ +package unipotsdam.gf.munchkin; + +import unipotsdam.gf.munchkin.model.Munschkin; + +import javax.ws.rs.PathParam; + +/** + * Created by dehne on 25.04.2018. + */ +public interface IMunschkin { + + Munschkin getMunschkin (int id); + void letMunchKinFight(Munschkin otherMunchkin); +} diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/controller/MunschkinLoader.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/controller/MunchkinImpl.java similarity index 84% rename from gemeinsamforschen/src/main/java/unipotsdam/gf/controller/MunschkinLoader.java rename to gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/controller/MunchkinImpl.java index ed7f0c22..65a0f2ba 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/controller/MunschkinLoader.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/controller/MunchkinImpl.java @@ -1,13 +1,14 @@ -package unipotsdam.gf.controller; +package unipotsdam.gf.munchkin.controller; -import unipotsdam.gf.model.Munschkin; +import unipotsdam.gf.munchkin.IMunschkin; +import unipotsdam.gf.munchkin.model.Munschkin; import java.sql.*; /** * Created by dehne on 24.04.2018. */ -public class MunschkinLoader { +public class MunchkinImpl implements IMunschkin { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost"; @@ -17,8 +18,9 @@ public class MunschkinLoader { static final String PASS = "voyager2"; // Es gibt natürlich auch libraries, die den Datenbankzugriff einfacher gestalten. Ziel soll sein, dass alle - // die Basics können. - public Munschkin loadMunschkin(int id) { + // die Basics können. Es ist auch sinnvoll, die Datenbankzugriffe in eine eigene Klasse auszulagern!! + @Override + public Munschkin getMunschkin(int id) { Munschkin munschkin = new Munschkin(); @@ -80,4 +82,9 @@ public class MunschkinLoader { return munschkin; } + + @Override + public void letMunchKinFight(Munschkin otherMunchkin) { + // TODO was euer Algorithmus sonst noch so kann + } } diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/model/Munschkin.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/model/Munschkin.java similarity index 98% rename from gemeinsamforschen/src/main/java/unipotsdam/gf/model/Munschkin.java rename to gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/model/Munschkin.java index e68d85c1..68a31988 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/model/Munschkin.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/model/Munschkin.java @@ -1,4 +1,4 @@ -package unipotsdam.gf.model; +package unipotsdam.gf.munchkin.model; import javax.xml.bind.annotation.XmlRootElement; diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/service/RestyMunschkin.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/service/RestyMunschkin.java similarity index 64% rename from gemeinsamforschen/src/main/java/unipotsdam/gf/service/RestyMunschkin.java rename to gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/service/RestyMunschkin.java index fbe92871..8ae8d662 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/service/RestyMunschkin.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/munchkin/service/RestyMunschkin.java @@ -1,11 +1,12 @@ -package unipotsdam.gf.service; +package unipotsdam.gf.munchkin.service; /** * Created by dehne on 24.04.2018. */ -import unipotsdam.gf.controller.MunschkinLoader; -import unipotsdam.gf.model.Munschkin; +import unipotsdam.gf.munchkin.IMunschkin; +import unipotsdam.gf.munchkin.controller.MunchkinImpl; +import unipotsdam.gf.munchkin.model.Munschkin; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -21,8 +22,8 @@ public class RestyMunschkin { @Produces(MediaType.TEXT_HTML) @Path("/hello/{id}") public String sayHtmlHello(@PathParam("id") String id) { - MunschkinLoader munschkinLoader = new MunschkinLoader(); - Munschkin munschkin = munschkinLoader.loadMunschkin(Integer.parseInt(id)); + IMunschkin munchkinImpl = new MunchkinImpl(); + Munschkin munschkin = munchkinImpl.getMunschkin(Integer.parseInt(id)); return "<html> " + "<title>" + "Hello Munschkin" + "</title>" + "<body><h1>" + munschkin .toString() + "</body></h1>" + "</html> "; } @@ -32,8 +33,8 @@ public class RestyMunschkin { @Produces(MediaType.APPLICATION_XML) @Path("/munschkin/{id}") public Munschkin getMunschkin(@PathParam("id") String id) { - MunschkinLoader munschkinLoader = new MunschkinLoader(); - Munschkin munschkin = munschkinLoader.loadMunschkin(Integer.parseInt(id)); + IMunschkin munchkinImpl = new MunchkinImpl(); + Munschkin munschkin = munchkinImpl.getMunschkin(Integer.parseInt(id)); return munschkin; } diff --git a/gemeinsamforschen/src/test/java/MunschkinLoaderTest.java b/gemeinsamforschen/src/test/java/MunschkinLoaderTest.java index e4bb28aa..41934e11 100644 --- a/gemeinsamforschen/src/test/java/MunschkinLoaderTest.java +++ b/gemeinsamforschen/src/test/java/MunschkinLoaderTest.java @@ -1,5 +1,6 @@ -import unipotsdam.gf.controller.MunschkinLoader; -import unipotsdam.gf.model.Munschkin; +import unipotsdam.gf.munchkin.IMunschkin; +import unipotsdam.gf.munchkin.controller.MunchkinImpl; +import unipotsdam.gf.munchkin.model.Munschkin; import org.junit.Test; /** @@ -8,8 +9,11 @@ import org.junit.Test; public class MunschkinLoaderTest { @Test public void testMunschKinLoader() { - MunschkinLoader m = new MunschkinLoader(); - Munschkin loadedM = m.loadMunschkin(1); + IMunschkin m = new MunchkinImpl(); + // Nützlich weil: IMunschkin m2 = new HendriksMunchkinImpl(); + + // ab hier ist es dem Code egal, welche Implementation hinter dem Interface steht + Munschkin loadedM = m.getMunschkin(1); assert loadedM != null; System.out.print(loadedM.toString()); } -- GitLab