|
Re: Using only 1 instance of an object
|
Posted: Dec 13, 2006 3:14 AM
|
|
Keeping all constructors private and adding some static methods and variables would be onen asty way to do it:
public class Map {
private Map(...) {
}
public static Map createMap(...) {
if (staticMap == null)
staticMap = new Map(...);
return staticMap;
}
private static staticMap = null;
}
So everytime someone wants to create the map, he calls.
Map myMap = Map.createMap(...);
instead of
Map myMap = new Map(...);
You could of course keep your reference to the map of course and pass it to everyone who needs it, like you mentioned first.
For all the Icons i use in my softwares, I implemented a provider which works basically like the method above. I pass the icon id to the provider. That provider looks if the icon was allready loaded and loads it if needed. If not it returns the icon which was loaded earlier.
|
|