Skip to content
Snippets Groups Projects
Commit b767017e authored by Julian Dehne's avatar Julian Dehne
Browse files

added Interfaces to munchkin example

parent f2973259
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,18 @@ ...@@ -8,6 +8,18 @@
<artifactId>gemeinsamforschen</artifactId> <artifactId>gemeinsamforschen</artifactId>
<packaging>war</packaging> <packaging>war</packaging>
<version>1.0-SNAPSHOT</version> <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> <properties>
<org.springframework.version>3.2.4.RELEASE</org.springframework.version> <org.springframework.version>3.2.4.RELEASE</org.springframework.version>
......
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);
}
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.*; import java.sql.*;
/** /**
* Created by dehne on 24.04.2018. * 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 JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost"; static final String DB_URL = "jdbc:mysql://localhost";
...@@ -17,8 +18,9 @@ public class MunschkinLoader { ...@@ -17,8 +18,9 @@ public class MunschkinLoader {
static final String PASS = "voyager2"; static final String PASS = "voyager2";
// Es gibt natürlich auch libraries, die den Datenbankzugriff einfacher gestalten. Ziel soll sein, dass alle // Es gibt natürlich auch libraries, die den Datenbankzugriff einfacher gestalten. Ziel soll sein, dass alle
// die Basics können. // die Basics können. Es ist auch sinnvoll, die Datenbankzugriffe in eine eigene Klasse auszulagern!!
public Munschkin loadMunschkin(int id) { @Override
public Munschkin getMunschkin(int id) {
Munschkin munschkin = new Munschkin(); Munschkin munschkin = new Munschkin();
...@@ -80,4 +82,9 @@ public class MunschkinLoader { ...@@ -80,4 +82,9 @@ public class MunschkinLoader {
return munschkin; return munschkin;
} }
@Override
public void letMunchKinFight(Munschkin otherMunchkin) {
// TODO was euer Algorithmus sonst noch so kann
}
} }
package unipotsdam.gf.model; package unipotsdam.gf.munchkin.model;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
......
package unipotsdam.gf.service; package unipotsdam.gf.munchkin.service;
/** /**
* Created by dehne on 24.04.2018. * Created by dehne on 24.04.2018.
*/ */
import unipotsdam.gf.controller.MunschkinLoader; import unipotsdam.gf.munchkin.IMunschkin;
import unipotsdam.gf.model.Munschkin; import unipotsdam.gf.munchkin.controller.MunchkinImpl;
import unipotsdam.gf.munchkin.model.Munschkin;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
...@@ -21,8 +22,8 @@ public class RestyMunschkin { ...@@ -21,8 +22,8 @@ public class RestyMunschkin {
@Produces(MediaType.TEXT_HTML) @Produces(MediaType.TEXT_HTML)
@Path("/hello/{id}") @Path("/hello/{id}")
public String sayHtmlHello(@PathParam("id") String id) { public String sayHtmlHello(@PathParam("id") String id) {
MunschkinLoader munschkinLoader = new MunschkinLoader(); IMunschkin munchkinImpl = new MunchkinImpl();
Munschkin munschkin = munschkinLoader.loadMunschkin(Integer.parseInt(id)); Munschkin munschkin = munchkinImpl.getMunschkin(Integer.parseInt(id));
return "<html> " + "<title>" + "Hello Munschkin" + "</title>" + "<body><h1>" + munschkin return "<html> " + "<title>" + "Hello Munschkin" + "</title>" + "<body><h1>" + munschkin
.toString() + "</body></h1>" + "</html> "; .toString() + "</body></h1>" + "</html> ";
} }
...@@ -32,8 +33,8 @@ public class RestyMunschkin { ...@@ -32,8 +33,8 @@ public class RestyMunschkin {
@Produces(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML)
@Path("/munschkin/{id}") @Path("/munschkin/{id}")
public Munschkin getMunschkin(@PathParam("id") String id) { public Munschkin getMunschkin(@PathParam("id") String id) {
MunschkinLoader munschkinLoader = new MunschkinLoader(); IMunschkin munchkinImpl = new MunchkinImpl();
Munschkin munschkin = munschkinLoader.loadMunschkin(Integer.parseInt(id)); Munschkin munschkin = munchkinImpl.getMunschkin(Integer.parseInt(id));
return munschkin; return munschkin;
} }
......
import unipotsdam.gf.controller.MunschkinLoader; import unipotsdam.gf.munchkin.IMunschkin;
import unipotsdam.gf.model.Munschkin; import unipotsdam.gf.munchkin.controller.MunchkinImpl;
import unipotsdam.gf.munchkin.model.Munschkin;
import org.junit.Test; import org.junit.Test;
/** /**
...@@ -8,8 +9,11 @@ import org.junit.Test; ...@@ -8,8 +9,11 @@ import org.junit.Test;
public class MunschkinLoaderTest { public class MunschkinLoaderTest {
@Test @Test
public void testMunschKinLoader() { public void testMunschKinLoader() {
MunschkinLoader m = new MunschkinLoader(); IMunschkin m = new MunchkinImpl();
Munschkin loadedM = m.loadMunschkin(1); // 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; assert loadedM != null;
System.out.print(loadedM.toString()); System.out.print(loadedM.toString());
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment