Monday, July 7, 2014

Flyweight Pattern in Java


Simple Note

From reference "Flyweight pattern":
flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. Often some parts of the object state can be shared, and it is common practice to hold them in external data structures and pass them to the flyweight objects temporarily when they are used.


Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/flyweight

Run TestMain.java to test.

Assume you need to calculate lots of sum of a range of Fibonacci Numbers.
There are 2 shared items in FibRangedSum:
1. Calculated results (a Map, with Multiton Pattern)
2. Fibonacci sequence (a List, not Multiton Pattern but still Flyweight Pattern)

References:

Flyweight pattern

Fibonacci number

No comments:

Post a Comment