Hi, Christian,

Thanks for the response! Well, there are points in your comment I could agree on. Yes, the example is very straightforward and simple, hence the difference between using the pattern and not using it is very marginal. So what you said about the possible implementation without using any factories or other classes — that’s true, indeed.

Anyway, by applying the Flyweight design pattern, you obtain some benefits:

  • It provides flexibility and extensibility (as pretty much any other design pattern) — an additional layer of abstraction increases the code complexity (e.g. the point you have mentioned about the factory), but your code becomes easily extensible, for instance, adding other shapes would be just as simple as adding a new class which implements the IPositionedShape interface;
  • As I have already mentioned, the shapes example in the article is very simple, just enough to grasp the main idea of the pattern. If you want a better example: the Flyweight design pattern is widely used in the video games industry when storing all the needed assets, complex graphical objects. Let’s take Minecraft as an example. Now imagine that for every visible block in the game you need to create a separate object for it and store in the memory — it is even hard to imagine how much RAM you would need to run the game on your local machine! But, of course, you could optimise that and store only a different kind of blocks in the memory and reuse them when building the limitless world. And that is not just a single position variable which could be stored differently, but a lot of different parameters for each different block — position, colour, texture, hardness, you name it.

I hope the idea behind this example and general implementation is more clear for you now, Christian.

Once again, thanks for your response, I am very glad to hear the feedback from you guys!

Written by

Software Engineer @ Devbridge | Flutter Enthusiast https://www.linkedin.com/in/mangirdas-kazlauskas/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store