powiedzmy, że mam taką immutable klasę z dwoma polami. wiem, że będzie z 200 zduplikowanych obiektów custom. dwa pytania:
- jak napisać prawidłowy cache w takim wypadku? -bez użycia Guavy itd
- czy to ma sens przy 200 zduplikowanych obiektach?
import java.util.HashMap;
import java.util.Map;
public class Wall {
private static final Map<Integer, Wall> cache = new HashMap<>();
private static final Wall STANDARD = new Wall(10, 10);
public final int height;
public final int length;
private Wall(int height, int length) {
this.height = height;
this.length = length;
}
public static Wall standard() {
return STANDARD;
}
public static Wall custom(int height, int length) {
Wall wall = cache.get(height);
if (wall == null || wall.length != length) {
wall = new Wall(height, length);
cache.put(height, wall);
}
return wall;
}
}