令人困惑的标题,更长但更简单,你可以创建一个对象类,其中构造函数将创建该类的多个对象并将它们存储在静态对象arrayList中,所有这些都在一个类中?如果你不能在那个班级中制造一个静态的空白呢?
此致 Augustas
答案 0 :(得分:0)
你可以这样做,我会把原因留给你,但问问你自己一般是个好问题,为什么我这样做呢......
import java.util.ArrayList;
public class Static {
public static ArrayList<Foo> foos = new ArrayList<Foo>();
//this initializes your list of objects...
static
{
foos.add(new Foo("A"));
foos.add(new Foo("B"));
foos.add(new Foo("C"));
foos.add(new Foo("D"));
}
}
class Foo
{
String value;
public Foo(String v) { value = v; }
}
答案 1 :(得分:0)
我建议实施Flyweight Pattern。来自维基百科:
flyweight是一个通过与其他类似对象共享尽可能多的数据来最小化内存使用的对象;当一个简单的重复表示会使用不可接受的内存量时,它是一种大量使用对象的方法。
Java已经通过static
工厂方法Integer
方法在Integer#valueOf
类中实现了此功能,默认情况下IntegerCache
保留的内部Integer
类来自 - {3}} 128直到127.这是一个例子:
Integer i1, i2, i3;
i1 = 100; //it will call Integer with value of 100 from cache
i2 = Integer.valueOf(100); //it will call Integer with value of 100 from cache
i3 = new Integer(100); //creates a new instance
System.out.println(i1 == i2); //true
System.out.println(i1 == i3); //false
System.out.println(i1.equals(i2)); //true
System.out.println(i1.equals(i3)); //true
了解这一点,您可以创建自己的flyweight实现:
public class YourClass {
private final String name;
private final String description;
//more fields...
//making a private constructor in case you don't want other classes
//to create instance of this class carelessly
//like Integer
private YourClass(String name, String description) {
this.name = name;
this.description = description;
//probably more logic here
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
@Override
public int hashCode() {
//sample implementation
//it can be heavily improved
return name.hashCode();
}
@Override
public boolean equals(Object o) {
//sample implementation
//it MUST be heavily improved
if (o == this) return true;
if (!(o instanceof YourClass)) return false;
YourClass other = (YourClass)o;
return this.name.equals(other.getName());
}
//static flyweight manager
private static class YourClassFlyweight {
//cache with weak entries
//wrapped into a synchronized Map
static final Map<String, YourClass> cache =
Collections.synchronizedMap(
new WeakHashMap<String, YourClass>());
//if you don't want weak entries
//then just use a ConcurrentHashMap
//static final Map<String, YourClass> cache =
// new ConcurrentHashMap<String, YourClass>()));
private YourClassFlyweight() { }
}
//using Factory Method along with this flyweight implementation
public static YourClass create(String name, String description) {
//check if it's not created
if (YourClassFlyweight.cache.containsKey(name)) {
//if it is, then return current instance
return YourClassFlyweight.cache.get(name);
}
//otherwise, create the instance and add it into cache
synchronized(YourClassFlyweight.cache) {
YourClass newInstance = new YourClass(name, description);
YourClassFlyweight.cache.put(name, newInstance);
return newInstance;
}
}
}
基本测试:
YourClass ins1 = YourClass.create("Luiggi", "Mendoza");
YourClass ins2 = YourClass.create("Luiggi", "OtherLastName");
System.out.println(ins1.equals(ins2)); // true
System.out.println(ins1 == ins2); // unbelievably, true
另外,使用Map
代替缓存的某些实现,您可以使用真正的缓存库,例如ehcache。