Central model instance via singleton

This commit is contained in:
joss 2025-04-25 18:51:25 +02:00
parent ca92fd78c8
commit 1a9aa98c72
2 changed files with 19 additions and 10 deletions

View file

@ -17,13 +17,9 @@ public class Main {
* @param args Die Programmargumente * @param args Die Programmargumente
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Model m; HttpServer s = null;
m = new Model(); Model.getModel().fillMockData();
m.fillMockData();
/**HttpServer s = null;
try { try {
s = HttpServer.create(new InetSocketAddress("127.0.0.1", 8000), 0); s = HttpServer.create(new InetSocketAddress("127.0.0.1", 8000), 0);
@ -36,10 +32,10 @@ public class Main {
s.setExecutor(null); s.setExecutor(null);
s.start(); s.start();
} catch (IOException e) { } catch (IOException e) {
System.err.println("IOException bei der Erstellung des HTTP Servers. Stack Trace folgt."); System.err.println("IOException bei der Erstellung des HTTP Servers.");
e.printStackTrace(); e.printStackTrace();
return; return;
}**/ }
// schließen ist nicht nötig, da beim Beenden die ganze JVM zerstört wird // schließen ist nicht nötig, da beim Beenden die ganze JVM zerstört wird
} }

View file

@ -19,9 +19,21 @@ import java.util.HashMap;
*/ */
public class Model { public class Model {
// die Hilfsvariable für das Singleton-Pattern
private static Model model = null;
private HashMap<String, DataObject> objs; private HashMap<String, DataObject> objs;
private HashMap<String, String> relations; private HashMap<String, String> relations;
/**
* Singleton.
*/
public static Model getModel() {
if (model == null)
model = new Model();
return model;
}
/** /**
* Initialisiert ein leeres Model. * Initialisiert ein leeres Model.
*/ */
@ -56,8 +68,9 @@ public class Model {
g1.addMember("students/jotto5"); g1.addMember("students/jotto5");
g2.addMember("students/vnachn"); g2.addMember("students/vnachn");
g2.addMember("students/ltorva"); g2.addMember("students/ltorva");
g3.addMember("alovel"); g3.addMember("students/alovel");
g3.addMember("aturin"); g3.addMember("students/aturin");
g3.addMember("students/ltorva");
} }
/* /*