Java Design Pattern: Flyweight

Flyweight pattern is used for minimizing memory usage. What it does is sharing as much data as possible with other similar objects.

1. Flyweight Pattern Class Diagram


2. Flyweight Pattern Java Code

// Flyweight object interface
interface ICoffee {
    public void serveCoffee(CoffeeContext context);
// Concrete Flyweight object  
class Coffee implements ICoffee {
    private final String flavor;
    public Coffee(String newFlavor) {
        this.flavor = newFlavor;
        System.out.println("Coffee is created! - " + flavor);
    public String getFlavor() {
        return this.flavor;
    public void serveCoffee(CoffeeContext context) {
        System.out.println("Serving " + flavor + " to table " + context.getTable());
// A context, here is table number
class CoffeeContext {
   private final int tableNumber;
   public CoffeeContext(int tableNumber) {
       this.tableNumber = tableNumber;
   public int getTable() {
       return this.tableNumber;

CoffeeFactory: it only create a new coffee when necessary.

//The FlyweightFactory!
class CoffeeFactory {
    private HashMap<String, Coffee> flavors = new HashMap<String, Coffee>();
    public Coffee getCoffeeFlavor(String flavorName) {
        Coffee flavor = flavors.get(flavorName);
        if (flavor == null) {
            flavor = new Coffee(flavorName);
            flavors.put(flavorName, flavor);
        return flavor;
    public int getTotalCoffeeFlavorsMade() {
        return flavors.size();

//Waitress serving coffee

public class Waitress {
   //coffee array
   private static Coffee[] coffees = new Coffee[20];
   //table array
   private static CoffeeContext[] tables = new CoffeeContext[20];
   private static int ordersCount = 0;
   private static CoffeeFactory coffeeFactory;
   public static void takeOrder(String flavorIn, int table) {
	   coffees[ordersCount] = coffeeFactory.getCoffeeFlavor(flavorIn);
       tables[ordersCount] = new CoffeeContext(table);
   public static void main(String[] args) {
	   coffeeFactory = new CoffeeFactory();
       takeOrder("Cappuccino", 2);
       takeOrder("Cappuccino", 2);
       takeOrder("Regular Coffee", 1);
       takeOrder("Regular Coffee", 2);
       takeOrder("Regular Coffee", 3);
       takeOrder("Regular Coffee", 4);
       takeOrder("Cappuccino", 4);
       takeOrder("Cappuccino", 5);
       takeOrder("Regular Coffee", 3);
       takeOrder("Cappuccino", 3);
       for (int i = 0; i < ordersCount; ++i) {
       System.out.println("\nTotal Coffee objects made: " +  coffeeFactory.getTotalCoffeeFlavorsMade());

Check out the output below, coffee is served to 10 tables, but only 2 coffees are created ever!

Coffee is created! - Cappuccino
Coffee is created! - Regular Coffee
Serving Cappuccino to table 2
Serving Cappuccino to table 2
Serving Regular Coffee to table 1
Serving Regular Coffee to table 2
Serving Regular Coffee to table 3
Serving Regular Coffee to table 4
Serving Cappuccino to table 4
Serving Cappuccino to table 5
Serving Regular Coffee to table 3
Serving Cappuccino to table 3

Total Coffee objects made: 2

This example is changed based on wiki-flyweight*, it is improved to be more understandable.

Category >> Design Patterns Stories  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Chandra Ravi

    Thanks for sharing the great informative content, Keep doing the great work.

    Java Training In Velachery

  • rupali waghmare

    Thanks for sharing insightful tips on this blog. Keep up the good work. I will continue follow and learn more from you.I am glad to read about java programming . Thanks once again.

  • Mahitha Mahi

    hi, nice information is given in this blog. Thanks for sharing this type of information, it is so useful for me. nice work keep it up.
    for more info visit us on :

  • cathryn leno

    When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.

  • Tristan Yan

    Not only factory should be singleton, but also getCoffeeFlavor is not thread-safe

  • Vinayak Tyagi

    In a multi-threaded environment , should the factory be a singleton ?

  • Karthick M

    This is a not-to-the-point example, aided by some bad use of arrays. Just the Coffee factory and the statement that objects would be created if they did not exist in some cache would have been sufficient to explain flyweight.

  • Srini

    I am being coffee lover I like this example.