Structural -- Flyweight

--- Flyweight.java ---
package structural.flyweight;
public interface Flyweight {
void sampleOperation(Context context);
}
--- ConcreteFlyweight.java ---
package structural.flyweight;
public class ConcreteFlyweight implements Flyweight {
public ConcreteFlyweight(Object key) {
}
public void sampleOperation(Context context) {
}
}
--- FlyweightFactory.java ---
package structural.flyweight;
import java.util.HashMap;
public class FlyweightFactory {
private HashMap theFlyweights;
public FlyweightFactory() {
theFlyweights = new HashMap();
}
public Flyweight getConcreteFlyweight(Object key) {
if (theFlyweights.containsKey(key)) {
return (Flyweight) theFlyweights.get(key);
} else {
Flyweight newFlyweight = new ConcreteFlyweight(key);
theFlyweights.put(key, newFlyweight);
return newFlyweight;
}
}
}
--- Context.java ---
package structural.flyweight;
public class Context {
}

0 Comments:
Post a Comment
<< Home