Code: Select all
public class Brother {
private final Parent parent;
public Brother(Parent parent) {
this.parent = parent;
}
public void annoySister() {
parent.getSister().annoy();
}
}
public class Sister {
private final Parent;
public Sister(Parent parent) {
this.parent = parent;
}
public void annoy() {
System.out.println("I am annoyed");
}
public void parentsName() {
parent.getName();
}
}
public class Parent {
private final Brother brother;
private final Sister sister;
private final String name;
public Parent() {
this.name = "Johnny";
this.brother = new Brother(this);
this.sister = new Sister(this);
}
// getters
}
< /code>
Q1. [b] Wie kann dieser
Code: Select all
public class Main {
public static void main(String args) {
// somehow this seems to be like a container for all objects; ApplicationContext?
Parent parentInstance = new Parent();
parentInstance.getBrother().annoySister();
parentInstance.getSister().parentsName();
}
< /code>
Die Klasse des Elternteils sieht mich wie Container aus. Es enthält Instanzen aller Klassen, die erstellt werden, und bietet eine Möglichkeit, auf alle [/b] diese Klassen zuzugreifen. Wenn also beispielsweise eine neue Klassendatenbank
Q2 Ist das eine Art Designmuster? Danke