paint-brush
The Mapleton Patternby@drodil
1,511 reads
1,511 reads

The Mapleton Pattern

by Heikki HellgrenOctober 1st, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<em>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 </em><a href="https://medium.com/@drodil/c-telltales-series-3a8c74a31629" target="_blank"><em>here</em></a><em>!</em>

People Mentioned

Mention Thumbnail

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - The Mapleton Pattern
Heikki Hellgren HackerNoon profile picture

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:

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.