Flyweight pattern

Flyweight pattern

In flyweight pattern flyweight is object that helps to reduce memory use using sharing data as much possible with other kind of related objects. It is approach to use objects in big numbers where simple repeated representation may use unacceptable amount of memory and put system on hang. Frequently some parts of object state could be shared and it is good practice to hold them out in external data structures and pass them to flyweight objects temporarily whenever they will be use.

For good example usage of flyweight pattern is when we use data structures for graphical representation of characters in word processor. It would be desirable to have it for each character in document, glyph object containing its font metrics, font outline and other formatting data and this could amount to hundreds or probably thousands of bytes for each characters. On other side for each character there can be reference to a flyweight glyph object that is shared by every object instance of same character in document. Only position of each character will need to store internally.


  • Flyweight object interface
package com.javahonk.flyweight;

public interface CoffeeOrderInterface {
    public void coffeeServe(CoffeeOrderContextTest context);


  • ConcreteFlyweight object that creates ConcreteFlyweight
package com.javahonk.flyweight;

public class CoffeeFlavorTest implements CoffeeOrderInterface{
    private String flavor;

    public CoffeeFlavorTest(String newFlavor) {
        this.flavor = newFlavor;

    public String getFlavor() {
        return this.flavor;

    public void coffeeServe(CoffeeOrderContextTest context) {
        System.out.println("Serving Coffee flavor " + flavor
                + " to table number " + context.getTable());




  • Flyweight context object
package com.javahonk.flyweight;

public class CoffeeOrderContextTest {
    private int tableNumber;

    public CoffeeOrderContextTest(int tableNumber) {
        this.tableNumber = tableNumber;

    public int getTable() {
        return this.tableNumber;


  • FlyweightFactory object
package com.javahonk.flyweight;

import java.util.HashMap;
import java.util.Map;

public class CoffeeFlavorFactoryPattern {
    private Map<String, CoffeeFlavorTest> flavors = 
            new HashMap<String, CoffeeFlavorTest>();

    public CoffeeFlavorTest getCoffeeFlavor(String flavorName) {
    CoffeeFlavorTest flavor = flavors.get(flavorName);
    if (flavor == null) {
        flavor = new CoffeeFlavorTest(flavorName);
        flavors.put(flavorName, flavor);
    return flavor;

    public int getTotalCoffeeFlavorsMade() {
    return flavors.size();


  • TestFlyweight
package com.javahonk.flyweight;

public class FlywieghtTestClass {

    private static CoffeeFlavorTest[] flavors = 
            new CoffeeFlavorTest[100];
    private static CoffeeOrderContextTest[] tables = 
            new CoffeeOrderContextTest[100];
    private static int ordersMade = 0;
    private static CoffeeFlavorFactoryPattern flavorFactory;

    public static void takeOrders(String flavorIn, int table) {
    flavors[ordersMade] = flavorFactory.getCoffeeFlavor(flavorIn);
    tables[ordersMade++] = new CoffeeOrderContextTest(table);

    public static void main(String[] args) {
    flavorFactory = new CoffeeFlavorFactoryPattern();
    takeOrders("Cappuccino", 2);
    takeOrders("Cappuccino", 2);
    takeOrders("Frappe", 1);
    takeOrders("Frappe", 1);
    takeOrders("Xpresso", 1);
    takeOrders("Frappe", 897);
    takeOrders("Cappuccino", 97);
    takeOrders("Cappuccino", 97);
    takeOrders("Frappe", 3);
    takeOrders("Xpresso", 3);
    takeOrders("Cappuccino", 3);
    takeOrders("Xpresso", 96);
    takeOrders("Frappe", 552);
    takeOrders("Cappuccino", 121);
    takeOrders("Xpresso", 121);
    for (int i = 0; i < ordersMade; ++i) {
    System.out.println(" ");
    System.out.println("total CoffeeFlavor objects made: "
            + flavorFactory.getTotalCoffeeFlavorsMade());



Flyweight pattern

  • That’s it. Below is list of all design patterns link:

Creational Patterns:

Structural Patterns:

Behavioral Patterns:

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *