Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
This is sixth part of my C++ Telltales series where I share some tips and tricks to work with C++. Feel free to check out also other parts of the series here!
Singleton pattern is used to create a class that allows sharing the same instance of a class for multiple users. It’s useful when you want to store some shared data in a class (for example configuration values) that can be easily accessed and are non-changing between different callers. Singleton pattern is pretty simple to implement:
While singleton might be enough for most of the cases, you might also sometimes need multiple instances. This is where Mapleton pattern comes in. It can provide instances of the class with for example unique identifier string. Each of the instances will have their own member variables that are not shared between other instances. Simply put, the Mapleton can be implemented as follows:
It’s not always good to use these patterns though and one might think they are even evil. Sometimes they make sense so always consider if you really need a singleton or if you can handle the task at hand. And if you are using multi-threading, always remember to protect changes to member variables accordingly!
And here is the same image from my first part to get some cover photo for this article (still lazy):
If you liked the story, please press the ❤ button below (did you know that you can give more than one clap). Also please feel free to share this story!
About me
I am Heikki Hellgren, Software Expert and technology enthusiast working at Elektrobit Automotive. My interests are in software construction, tools, automatic testing and all the new and cool stuff like AI and autonomous driving. You can follow me on Medium and Twitter and check out my websitefor more information.
The Mapleton Pattern was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.