I think what Sproingie meant is that while you could just write in FooFactory.getFlyweight(id) everywhere you’re going to need to, that it’s not always a good thing to do so. Passing it like a parameter would be better, but as he said it would require the clunkiness of the Class class (Har har) to do so if all of the access is through static methods.
What I think Sproingie meant.
public class StaticTester {
public static void doOp() {
System.out.println("Op is did.");
}
}
public class StaticExample {
StaticTester staticObject;
public StaticExample(StaticTester staticObject) {
this.staticObject = staticObject;
}
private StaticExample(Class<StaticTester> aClass) throws InstantiationException, IllegalAccessException {
this.staticObject = aClass.newInstance();
}
public void doOp() {
System.out.println("Example did.");
staticObject.doOp();
System.out.println("Example did again.");
}
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
StaticExample se = new StaticExample(StaticTester.class);
se.doOp();
}
}
Using static methods also adds in the problem of how the static fields are initialized…
Do you have to call FooFactory.initialize() first? Which would mean that you would have to call FooFactory.initialize() prior to it being used anywhere and probably put in checks for many of the functions you’d use with it to ensure that it was initialized.
public class StaticTester {
private static boolean isInit = false;
private static String say;
public static void initialize() {
say = "We say this.";
isInit = true;
}
public static void doOp() {
if(isInit) {
System.out.println(say);
} else {
throw new ClassNotInitializedException("This is made up!");
}
}
}
Do you use a static initialization block in the FooFactory code? However, this takes away a lot of your ability to decide when/where/how the information gets initialized as it is called when the class is first imported or used (Not completely certain there, but you don’t have control).
public class StaticTester {
private static String say;
static {
say = "We say this.";
}
public static void doOp() {
System.out.println(say);
}
}
Having another class create it, or make the first call to some static method in FooFactory to get an instance of FooFactory, then passing it to each object takes care of those issues. It also makes replacing the factory easier (Going from static calls like FooFactory.method() to object.method() can be fun.)
public class StaticExample {
StaticTester staticObject;
public StaticExample(StaticTester staticObject) {
this.staticObject = staticObject;
}
public void doOp() {
System.out.println("Example did.");
System.out.println(staticObject.getInitializationVariable());
System.out.println("Example did again.");
}
public static void main(String[] args) {
StaticTester.createInstance("New stringer string!");
StaticExample se = new StaticExample(StaticTester.getInstance());
se.doOp();
}
}
public class StaticTester {
private static StaticTester instance;
public static void createInstance(String initializationVariable) {
instance = new StaticTester(initializationVariable);
}
public static StaticTester getInstance() {
if(instance == null) {
throw new ClassNotInitializedException("This is made up!");
} else {
return instance;
}
}
private String initializationVariable;
private StaticTester(String initializationVariable) {
this.initializationVariable = initializationVariable;
}
public String getInitializationVariable() {
return initializationVariable;
}
}
PS-- If someone can tell me how to get Spoilers and Code to work together I’ll edit this. Also, if it’d be better to put all of these code snippets elsewhere do say so!