eEcho blog

is een halte van de gedachte

Flyweight pattern

Flyweight is a software design pattern. A Flyweight is an object that minimizes memory occupation by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple representation would use an unacceptable amount of memory. Often some parts of the object state can be shared and it’s common to put them in external data structures and pass them to the flyweight objects temporarily when they are used.

A classic example usage of the flyweight pattern are the data structures for graphical representation of characters in a word processor. It would be nice to have, for each character in a document, a glyph object containing its font outline, font metrics, and other formatting data, but it would amount to hundreds or thousands of bytes for each character. Instead, for every character there might be a reference to a flyweight glyph object shared by every instance of the same character in the document; only the position of each character (in the document and/or the page) would need to be stored externally.

public enum FontEffect {
    BOLD, ITALIC, SUPERSCRIPT, SUBSCRIPT, STRIKETHROUGH
}

public final class FontData {
    /**
    * A weak hash map will drop unused references to FontData.
    * Values have to be wrapped in WeakReferences,
    * because value objects in weak hash map are held by strong references.
    */
    private static final WeakHashMap<FontData, WeakReference<FontData>> flyweightData =
       new WeakHashMap<FontData, WeakReference<FontData>>();
    private final int pointSize;
    private final String fontFace;
    private final Color color;
    private final Set<FontEffect> effects;

    private FontData(int pointSize, String fontFace, Color color, EnumSet<FontEffect> effects) {
       this.pointSize = pointSize;
       this.fontFace = fontFace;
       this.color = color;
       this.effects = Collections.unmodifiableSet(effects);
    }

    public static FontData create(int pointSize, String fontFace, Color color,
       FontEffect… effects) {
       EnumSet<FontEffect> effectsSet = EnumSet.noneOf(FontEffect.class);
       for (FontEffect fontEffect : effects) {
            effectsSet.add(fontEffect);
       }
       // We are unconcerned with object creation cost, we are reducing overall memory consumption
       FontData data = new FontData(pointSize, fontFace, color, effectsSet);
       if (!flyweightData.containsKey(data)) {
            flyweightData.put(data, new WeakReference(data));
       }
       // return the single immutable copy with the given values
       return flyweightData.get(data).get();
    }

    @Override
    public boolean equals(Object obj) {
       if (obj instanceof FontData) {
            if (obj == this) {
                return true;
            }
            FontData other = (FontData) obj;
            return other.pointSize == pointSize && other.fontFace.equals(fontFace)
                && other.color.equals(color) && other.effects.equals(effects);
       }
       return false;
    }

    @Override
    public int hashCode() {
       return (pointSize * 37 + effects.hashCode() * 13) * fontFace.hashCode();
    }

    // Getters for the font data, but no setters. FontData is immutable.
}

Comments are closed.

Home | info@eecho.info | Voorwaarden | Blog