Flyweight Pattern Implementation Example

Hey everyone,

I’m learning mostly used design patterns for game development. I made an example about the flyweight pattern. Did I implement pattern correctly?

There is 2 different block that names are A and B. As a variable name, height and width are same some objects but point is different for every blocks.

Thanks for help :slight_smile:

// Block.cs
    namespace Oyun_Programalama_Tasarım_Desenleri
    {
      public abstract class Block
      {
        protected string name;
        protected int height;
        protected int width;
        protected string point; // different for every blocks.
  
        public abstract void Display(string point);
      }
    }
// BlockA.cs
    namespace Oyun_Programalama_Tasarım_Desenleri
    {
      public class BlockA : Block
      {
        public BlockA()
        {
          name = "A";
          height = 16;
          width = 16;
        }
  
        public override void Display(string point)
        {
          // point is different for every block
          this.point = point;
          Console.WriteLine(name + " " + height + " " + width + " " + this.point);
        }
      }
    }
    // BlockB.cs
    namespace Oyun_Programalama_Tasarım_Desenleri
    {
      public class BlockB : Block
      {
        public BlockB()
        {
          name = "B";
          height = 16;
          width = 16;
        }
  
        public override void Display(string point)
        {
          // point is different for every block
          this.point = point;
          Console.WriteLine(name + " " + height + " " + width + " " + this.point);
        }
      }
    }
// BlockFactory.cs
    namespace Oyun_Programalama_Tasarım_Desenleri
    {
      public class BlockFactory
      {
        private Dictionary<char, Block> blocks = new Dictionary<char, Block>();
        public Block GetBlock(char key)
        {
          Block block = null;
  
          if (blocks.ContainsKey(key))
          {
            block = blocks[key];
          }
          else
          {
            switch (key)
            {
              case 'A': block = new BlockA(); break;
              case 'B': block = new BlockB(); break;
            }
            blocks.Add(key, block);
          }
          return block;
        }
      }
    }
// Program.cs
    namespace Oyun_Programalama_Tasarım_Desenleri
    {
      class Program
      {
        static void Main(string[] args)
        {
          char key;
  
          BlockFactory blockFactory = new BlockFactory();
  
          Block blockA = blockFactory.GetBlock('A');
          blockA.Display("1");
  
          Block blockB = blockFactory.GetBlock('B');
          blockB.Display("2");
  
  
          Console.ReadKey();
        }
      }
    }

You have the right theory but the wrong implementation. All of your shared data is unique to each instance of BlockA and BlockB, it’s simply inherited from the base Block. Here’s a semi pseudo-code implementation:

class BlockTemplate
{
   public float width { get; private set; }
   public float height { get; private set ;}

   public BlockTemplate(float width, float height) { this.width = width; this.height = height; }
}

class Block
{
   private BlockTemplate template;
   public string name { get; }

   public Block(BlockTemplate template, string name) { this.template = template; this.name = name; }

   public override string ToString()
   {
       return string.Format("width: {0}, height {1}, name: {2}", template.width, template.height, name);
   }
}

Here each 16x16 block can share the same template dimensions but have a unique name, assuming your BlockFactory ensured only one template for each requested set of dimensions was created.

1 Like

I understand. thanks for the explanation.

A thing to know about formal Design Patterns is they were thought up 25 years ago for strong Object Oriented programming. No one uses that any more so many design patterns are obsolete.

The code you wrote looks like it’s from Factory or AbstractFactory. Today, those are smushed together to mean “any object-creation function or class where you can plug-in different ones to make different things, without users needing to change anything”. Another one: State Machines are useful; but the State design pattern is just a funny way to make them in a more Object-Oriented way (which, again, no one cares about now). Use State Machines, but skip their design pattern".

Flyweight is actually a fine one. But it’s just another word for factoring data. It’s easier to understand it if you read simple stuff about databases and how to break things into tables. I had to read through official “Flyweight” (from the book) several times to figure it out, and only since I already know databases.

1 Like