(und diese Logik würde ich auslagern, z.B. in Utility-Classes).
Klassen mit privatem Konstruktor und statischen Methoden,
public class Foo {
private final String foo;
private final Bar bar;
public Foo(String foo) {
this.foo = foo;
this.bar = BarUtilities.newBar(String foo, this);
}
}
public class Bar {
...
public Bar(Foo foo, String eins, String zwei, String 3) { ... }
...
}
public class BarUtilities {
private BarUtilities() {}
public static final Bar newBar(String foo, Foo foo) {
String[] split = foo.split(".");
if (split.length <> 3) {
throw new IllegalArgumentException("...");
}
if (split[0].equals("test")) {
return new Bar("passed", split[1], split[2], foo);
} else {
return new Bar(split[0], split[1], split[2], foo);
}
}
public class Foo {
private final String foo;
private final Bar bar;
public Foo(String foo) {
this.foo = foo;
this.bar = BarUtilities.newBar(foo, this);
}
}