The Flyweight Design Pattern is one of the structural patterns introduced by the GOF. The Flyweight Pattern is about creating a pool of objects that allows sharing already existing objects and causing the application to consume less memory.
- Increases application performance in terms of object creation since there is no requirement of creating an object for every request.
- Causes the application to consume less memory, since the object already exists in memory and no new object must be created because of the pool.
Ultimately flyweight patterns make the application efficient in terms of memory and processing.
Basic UML class Diagram of Design Pattern
The following image shows the class diagram of the basic Flyweight Design Pattern.
- IFlyweight: Basic contract to be implemented by derived types, in other words by concrete flyweight.
- FlyweightFactory: It's a factory class used by a client class to get the data from the concreate flyweight. This class is also responsible for creating the pool of an existing object. So when the request for data come that is already requested it returns data from the pool.
- ConcerateSharedFlyweight1: It's a concreate implementation of the flyweight interface. As the name suggests, an object returned by this class will be shared among the clients of it.
- ConcerateUnSharedFlyweight1: It's a concreate implementation of the flyweight interface. As the name suggests an object returned by this class will be unshared. That means that every time the client requires a new object.
- Client: uses the concreate implementation, it creates an instance of Decorate and uses the functionality of it.
Note: UnSharedFlyweight is not always required, it depends on requirements, but SharedFlyweight is always required when you use the flyweight pattern.
The following code is the implementation of the Flyweight Design Pattern and the Class Diagram was shown above.
- namespace BasicFlyweightPattern
- {
- #region basic implementation
- public class FlyweightFactory
- {
- public static List<Item> GetStaticItemList(string key)
- {
- IFlyWeight flyWeight = null;
- ICacheManager _objCacheManager = CacheFactory.GetCacheManager();
- if (key == "A")
- {
- if (_objCacheManager.Contains("A"))
- {
- return _objCacheManager["A"] as List<Item>;
- }
- else
- {
- flyWeight = new ConcerateSharedFlyweight1();
- }
- }
- else if (key == "B")
- {
- if (_objCacheManager.Contains("B"))
- {
- return _objCacheManager["B"] as List<Item>;
- }
- else
- {
- flyWeight = new ConcerateSharedFlyweight2();
- }
- }
- var list = flyWeight.GetList();
- _objCacheManager.Add(key, list);
- return list;
- }
- }
- interface IFlyWeight
- {
- List<Item> GetList();
- }
- public class Item
- {
- public int id { get; set; }
- public string desc { get; set; }
- }
- public class ConcerateSharedFlyweight1 : IFlyWeight
- {
- private List<Item> ItemList;
- public ConcerateSharedFlyweight1()
- {
- ItemList = new List<Item>();
- }
- public List<Item> GetList()
- {
- ItemList.Add(new Item { id = 1, desc = "A1" });
- ItemList.Add(new Item { id = 2, desc = "A2" });
- ItemList.Add(new Item { id = 3, desc = "A3" });
- return ItemList;
- }
- }
- public class ConcerateSharedFlyweight2 : IFlyWeight
- {
- private List<Item> ItemList;
- public ConcerateSharedFlyweight2()
- {
- ItemList = new List<Item>();
- }
- public List<Item> GetList()
- {
- ItemList.Add(new Item { id = 1, desc = "B1" });
- ItemList.Add(new Item { id = 2, desc = "B2" });
- ItemList.Add(new Item { id = 3, desc = "B3" });
- return ItemList;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- List<Item> list =FlyweightFactory.GetStaticItemList("A");
- //List<Item> list = FlyweightFactory.GetStaticItemList("B");
- foreach (var item in list)
- {
- Console.WriteLine(item.id.ToString() + " " + item.desc );
- }
- Console.ReadLine();
- }
- }
- }




Priyaranjan K SPosted Oct 26, 2015, 11:53 AM
Nice Share
Karthikeyan KPosted Sep 7, 2015, 11:14 PM
Good one sir...Thanks for sharing
Pankaj Kumar ChoudharyPosted Sep 7, 2015, 12:30 PM
Nice Explain Sir..........
Mohammed IbrahimPosted Sep 7, 2015, 11:54 AM
nice information
Chervine BhiwooPosted Aug 9, 2015, 11:18 AM
Good solution!
Rajeesh MenothPosted Aug 9, 2015, 3:48 AM
Good One
Santhakumar MunuswamyPosted Aug 9, 2015, 1:28 AM
Good Article. Thanks for sharing
Gopi ChandPosted Aug 8, 2015, 11:50 PM
Well done
Ankit BansalPosted Aug 8, 2015, 11:27 PM
good explanation...thanks
Pankaj Kumar ChoudharyPosted Aug 8, 2015, 9:46 PM
Nice Explain Sir........