What is the need for Flyweight Pattern in C#?

If your application includes many primitive objects, then sharing them efficiently will be a tough task. This sharing of primitive objects is achieved using Flyweight pattern in C#.



You can share these primitive objects as many times as required by different set of Clients. This is demonstrated using the following code snippet:

class sampleFlyweightGrouping {
private Hashtable htFlyweight = new Hashtable();
public sampleFlyweightClass() {
htFlyweight.Add("sample1", new sampleFlyweightClass());
htFlyweight.Add("sample2", new sampleFlyweightClass());
htFlyweight.Add("sample3", new sampleFlyweightClass());
}
public Flyweight fetchPrimitiveObject(string keyForObject) {
return ((sampleFlyweightClass) htFlyweight[keyForObject]);
}
}

You can have your own method definitions for sampleFlyweightClass and then create a testClass. Inside Main method of testClass, create an instance of sampleFlyWeightGrouping. With this instance, you can call fetchPrimitiveObject for each of the primitive objects appended and then call appropriate methods of this object.

| What is the need for Bridge Pattern in C#? | What is the need for Builder Pattern in C#? | What is the need for Chain of Responsibility Pattern in C#? | What is the need for Command Pattern in C#? | What is the need for Composite Pattern in C#? | What is the need for Decorator Pattern in C#? | What is the need for Flyweight Pattern in C#? | What is the need for Interpreter Pattern in C#? | What is the need for Iterator Pattern in C#? | What is the need for Mediator Pattern in C#? | What is the need for Memento Pattern in C#? | What is the need for Prototype Pattern in C#? | What is the need for State Pattern in C#? | What is the need for Strategy Pattern in C#? | What is the need for Template Method Pattern in C#? | What is the need for unsafe code in C#? | What is the purpose of assert() in C#? | What is the purpose of AutoResetEvent in .NET? | What is the purpose of Console.ReadLine() in C#? | What is the purpose of machine.config file in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.