Flyweight Pattern is a structural pattern as it is used to form large object structures across many disparate objects. This pattern minimizes memory use by sharing as much data as possible with other similar objects. This pattern increases performance by avoid creating a large number of expensive objects and instead reusing the existing instances to represent new ones.

Class Diagram

Flyweight Pattern in Java

Implementation

We first create a class, IcecreamFlavour, which represents different flavours of icecream.

package com.roytuts.designpattern.flyweight;

public class IcecreamFlavour {

  private String flavourName;

  public IcecreamFlavour(String flavourName) {
    this.flavourName = flavourName;
  }

  @Override
  public String toString() {
    return flavourName;
  }

}

Next we create a class called IcecreamMenu which is having lookup method that creates a new icecream flavour if a flavour does not already exist and returns the icecream flavour.

package com.roytuts.designpattern.flyweight;

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

public class IcecreamMenu {

  private Map<String, IcecreamFlavour> map = new HashMap<String, IcecreamFlavour>();

  public IcecreamFlavour lookup(String flavourName) {
    if (!map.containsKey(flavourName)) {
      map.put(flavourName, new IcecreamFlavour(flavourName));
    }
    return map.get(flavourName);
  }

  public int totalIcecreamFlavours() {
    return map.size();
  }
}

Now we have below class to track orders

package com.roytuts.designpattern.flyweight;

public class IcecreamOrder {

  private final int tableNumber;
  private final IcecreamFlavour flavour;

  public IcecreamOrder(int tableNumber, IcecreamFlavour flavour) {
    this.tableNumber = tableNumber;
    this.flavour = flavour;
  }

  public void serveIcecream() {
    System.out.println("Serving Icecream flavour " + flavour + " to the table number " + tableNumber);
  }

}

Using the below class we will serve the icecream of different flavours

package com.roytuts.designpattern.flyweight;

import java.util.ArrayList;
import java.util.List;

public class IcecreamShop {

  private final List<IcecreamOrder> icecreamOrders = new ArrayList<IcecreamOrder>();
  private final IcecreamMenu icecreamMenu = new IcecreamMenu();

  private void getIcecreamOrder(String flavourName, int tableNumber) {
    IcecreamFlavour flavour = icecreamMenu.lookup(flavourName);
    IcecreamOrder order = new IcecreamOrder(tableNumber, flavour);
    icecreamOrders.add(order);
  }

  private void icecreamService() {
    for (IcecreamOrder icecreamOrder : icecreamOrders) {
      icecreamOrder.serveIcecream();
    }
  }

  private String report() {
    return "Total number of Icecream flavour served : " + icecreamMenu.totalIcecreamFlavours();
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    IcecreamShop icecreamShop = new IcecreamShop();
    icecreamShop.getIcecreamOrder("Vanilla", 2);
    icecreamShop.getIcecreamOrder("Butter Scotch", 15);
    icecreamShop.getIcecreamOrder("Cone", 7);
    icecreamShop.getIcecreamOrder("Mango", 3);
    icecreamShop.getIcecreamOrder("Apple", 5);
    icecreamShop.getIcecreamOrder("Pineapple", 4);
    icecreamShop.getIcecreamOrder("Two in One", 10);
    icecreamShop.getIcecreamOrder("Butter Milk", 12);
    icecreamShop.getIcecreamOrder("Lemon", 9);
    icecreamShop.getIcecreamOrder("Chocolate", 13);
    icecreamShop.icecreamService();
    System.out.println(icecreamShop.report());
  }

}

Run the above class and see the output as shown below

Output

Serving Icecream flavour Vanilla to the table number 2
Serving Icecream flavour Butter Scotch to the table number 15
Serving Icecream flavour Cone to the table number 7
Serving Icecream flavour Mango to the table number 3
Serving Icecream flavour Apple to the table number 5
Serving Icecream flavour Pineapple to the table number 4
Serving Icecream flavour Two in One to the table number 10
Serving Icecream flavour Butter Milk to the table number 12
Serving Icecream flavour Lemon to the table number 9
Serving Icecream flavour Chocolate to the table number 13
Total number of Icecream flavour served : 10

That’s all. Thanks for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

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