Aug
23
2010
23
2010
Design Patterns : Flyweight pattern
Motivation
There are cases in programming where it seems that you need to generate a very large number of small class instances to represent data. Sometimes you can greatly reduce the number of different classes that you need to instantiate if you can recognize that the instances are fundamentally the same except for a few parameters.
Intent
Use sharing to support large numbers of fine-grained objects efficiently.
Applicability
Use flyweight pattern when
- An application uses a large number of objects
- Storage costs are high because of the sheer quantity of objects
Note: Flyweights are not frequently used at the application level in Java. They are more of a system resource management technique, used at a lower level than Java.
Diagram

Code Sample
public abstract class TeaOrder {public abstract void serveTea(TeaOrderContext teaOrderContext);
}
Concrete Flyweight
public class TeaFlavor extends TeaOrder {String teaFlavor;
TeaFlavor(String teaFlavor) {this.teaFlavor = teaFlavor;
}
public String getFlavor() {return this.teaFlavor;
}
public void serveTea(TeaOrderContext teaOrderContext) { System.out.println("Serving tea flavor " + teaFlavor +" to table number " +
teaOrderContext.getTable());
}
}
Context // This class is only in the context of this example
public class TeaOrderContext {int tableNumber;
TeaOrderContext(int tableNumber) {this.tableNumber = tableNumber;
}
public int getTable() {return this.tableNumber;
}
}
The Factory
public class TeaFlavorFactory {TeaFlavor[] flavors = new TeaFlavor[10];
//no more than 10 flavors can be made
int teasMade = 0;
public TeaFlavor getTeaFlavor(String flavorToGet) { if (teasMade > 0) { for (int i = 0; i < teasMade; i++) { if (flavorToGet.equals((flavors[i]).getFlavor())) {return flavors[i];
}
}
}
flavors[teasMade] = new TeaFlavor(flavorToGet);
return flavors[teasMade++];
}
public int getTotalTeaFlavorsMade() {return teasMade;}}
The Client
class TestFlyweight {static TeaFlavor[] flavors =
new TeaFlavor[100];
//the flavors ordered
static TeaOrderContext[] tables =
new TeaOrderContext[100];
//the tables for the orders
static int ordersMade = 0;
static TeaFlavorFactory teaFlavorFactory;
static void takeOrders(String flavorIn, int table) {flavors[ordersMade] =
teaFlavorFactory.getTeaFlavor(flavorIn);
tables[ordersMade++] =
new TeaOrderContext(table);
}
public static void main(String[] args) {teaFlavorFactory = new TeaFlavorFactory();
takeOrders("chai", 2); takeOrders("chai", 2); takeOrders("camomile", 1); takeOrders("camomile", 1); takeOrders("earl grey", 1); takeOrders("camomile", 897); takeOrders("chai", 97); takeOrders("chai", 97); takeOrders("camomile", 3); takeOrders("earl grey", 3); takeOrders("chai", 3); takeOrders("earl grey", 96); takeOrders("camomile", 552); takeOrders("chai", 121); takeOrders("earl grey", 121); for (int i = 0; i < ordersMade; i++) {flavors[i].serveTea(tables[i]);
}
System.out.println(" "); System.out.println("total teaFlavor objects made: " +teaFlavorFactory.getTotalTeaFlavorsMade());
}
}
Related Posts
2 Comments + Add Comment
Recent Posts
- How to populate a dropdown menu from database in CodeIgniter [Workaround]
- How to Overclock Samsung Galaxy Mini Upto 800MHz
- Current and Future Relavance of Business Excellence / Improvement to Argos UK Ltd. Possible Benefits and Likely Inhabitors Exists
- What can Harm Your Online Reputation?
- Does Race Matter in the Choice of Popular Music among Youngsters?
The Tutebox
- Accounting
- Android Tips
- Apps
- Business
- C / C++
- Computers
- Database
- Design Patterns
- E-Commerce
- Economics
- Electronics
- Entertainment
- Finance
- Functional Programming
- Games
- Hardware
- Health
- HRM
- International Business
- Internet
- iOS Tips
- JAVA
- Life Style
- Linux
- M-Commerce
- Management
- Marketing
- Mathematics
- Mobile
- MS Office
- Network
- PC
- Personal Development
- Photography
- PHP
- Physics
- Places
- Programming
- Science
- Software
- Statistics
- Tweaks
- Visual Studio
- Web Hosting
- Windows

Article by





nice post. thanks.
Useful blog website, keep me personally through searching it, I am seriously interested to find out another recommendation of it.