Skip to content

Latest commit

 

History

History
116 lines (91 loc) · 5.17 KB

flyweight.md

File metadata and controls

116 lines (91 loc) · 5.17 KB

Flyweight

The Flyweight design pattern is a software design pattern that aims to minimize memory usage by sharing data among multiple objects. It is categorized as a structural design pattern and is often used in scenarios where a large number of objects need to be created and managed efficiently.

The main idea behind the Flyweight pattern is to separate intrinsic and extrinsic state in objects. Intrinsic state represents the data that is shared among multiple objects, while extrinsic state represents the data that is specific to each individual object.

The Flyweight pattern achieves memory optimization by creating a pool of shared flyweight objects and allowing multiple contexts (objects) to reference and use these flyweights. This reduces the memory footprint because instead of each object storing its own copy of the intrinsic state, they refer to the shared flyweight object.

By utilizing the Flyweight pattern, you can significantly reduce memory consumption, especially when dealing with a large number of similar objects.

It's important to note that the Flyweight pattern may introduce some trade-offs. Sharing state among objects can increase complexity, and there might be a slight performance overhead due to the indirection involved in accessing shared objects. Therefore, it's essential to carefully evaluate the use cases before applying this pattern.

Overall, the Flyweight pattern is a valuable technique for optimizing memory usage in scenarios where a large number of objects need to be managed efficiently by sharing common data.

Here's an example of how the Flyweight pattern can be implemented in C#:

using System;
using System.Collections.Generic;

// Flyweight interface
public interface ICharacterFlyweight
{
    void Display(int positionX, int positionY);
}

// Concrete flyweight class representing a character
public class CharacterFlyweight : ICharacterFlyweight
{
    private readonly char character;

    public CharacterFlyweight(char character)
    {
        this.character = character;
    }

    public void Display(int positionX, int positionY)
    {
        Console.WriteLine("Character: " + character + " at position (" + positionX + ", " + positionY + ")");
    }
}

// Flyweight factory
public class CharacterFlyweightFactory
{
    private Dictionary<char, ICharacterFlyweight> flyweights = new Dictionary<char, ICharacterFlyweight>();

    public ICharacterFlyweight GetCharacterFlyweight(char character)
    {
        if (flyweights.ContainsKey(character))
        {
            return flyweights[character];
        }
        else
        {
            ICharacterFlyweight flyweight = new CharacterFlyweight(character);
            flyweights.Add(character, flyweight);
            return flyweight;
        }
    }
}

// Context class representing a character instance on the screen
public class CharacterContext
{
    private readonly ICharacterFlyweight characterFlyweight;
    private readonly int positionX;
    private readonly int positionY;

    public CharacterContext(ICharacterFlyweight characterFlyweight, int positionX, int positionY)
    {
        this.characterFlyweight = characterFlyweight;
        this.positionX = positionX;
        this.positionY = positionY;
    }

    public void Display()
    {
        characterFlyweight.Display(positionX, positionY);
    }
}

// Example usage
public class Program
{
    public static void Main(string[] args)
    {
        CharacterFlyweightFactory flyweightFactory = new CharacterFlyweightFactory();

        // Create character contexts
        CharacterContext context1 = new CharacterContext(flyweightFactory.GetCharacterFlyweight('A'), 10, 20);
        CharacterContext context2 = new CharacterContext(flyweightFactory.GetCharacterFlyweight('B'), 15, 25);
        CharacterContext context3 = new CharacterContext(flyweightFactory.GetCharacterFlyweight('A'), 30, 40);

        // Display character contexts
        context1.Display();
        context2.Display();
        context3.Display();
    }
}

In this example, we have a CharacterFlyweight class that represents the flyweight object storing the intrinsic state (the character). The CharacterFlyweightFactory class is responsible for creating and managing the flyweight objects, ensuring that each character is shared appropriately.

The CharacterContext class represents the context objects that store the extrinsic state (the position) and reference the shared flyweight objects. The Display method in the CharacterContext class delegates the display functionality to the appropriate flyweight object.

In the Main method, we create the flyweight factory and use it to obtain the flyweight objects for different characters. We then create multiple character contexts with different positions and display them.

When you run this C# example, you'll see the output displaying the characters and their positions:

Character: A at position (10, 20)
Character: B at position (15, 25)
Character: A at position (30, 40)

Please note that this is a simplified example to demonstrate the basic implementation of the Flyweight pattern in C#. In a real-world scenario, there may be additional considerations and optimizations depending on the specific requirements of the application.