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); ordersCount++; } 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) { coffees[i].serveCoffee(tables[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.
* http://en.wikipedia.org/wiki/Flyweight_pattern
Wow, your blog on Java is truly insightful and comprehensive and I appreciate the way you seamlessly integrate front-end and back-end development principles.
Besides this, if you want to learn about Full stack Development enroll now at the bestFull Stack Developer Course in Gurgaon
Thanks for sharing this blog, really helps to understand the some concepts of Java programming language. For learn more about Java, Join Java course in Ghaziabad.
Thanks for sharing this blog, really helps to understand the some concepts of Java programming language. For learn more about Java, Join Java course in Ghaziabad.
Thanks for sharing the great informative content, Keep doing the great work.
Java Training In Velachery
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.
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 : http://www.supreetsolutions.com/
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.
http://www.traininginsholinganallur.in/amazon-web-services-training-in-chennai.html
http://www.traininginannanagar.in/amazon-web-services-training-in-chennai.html
Not only factory should be singleton, but also getCoffeeFlavor is not thread-safe
In a multi-threaded environment , should the factory be a singleton ?
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.
I am being coffee lover I like this example.