The Mapleton Pattern

C++ Telltales part 6:

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:

https://medium.com/media/888454cdfd4d9aa3bb765cd08cb8323f/href

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:

https://medium.com/media/ea4a70e6d81d68cb48dbaa3eaccbb29d/href

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.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: