Flywieght Pattern
이미 생성한 객체가 있으면 공유, 없으면 생성하여 메모리 절약.
Flyweight
public interface Flyweight {
String getData();
}
ConcreteFlyweight
public class ConcreteFlyweight implements Flyweight {
private String data;
public ConcreteFlyweight(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
FlyweightFactory
public enum FlyweightFactory {
INSTANCE; // Singleton
private Map<String, Flyweight> pool = new ConcurrentHashMap<>();
public Flyweight getFlyweight(String key) {
if(!pool.containsKey(key)) {
pool.put(key, new ConcreteFlyweight(key));
}
return pool.get(key);
}
}