Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

singleton
monostate
design pattern

What is the alternative to the singleton design pattern?

Osamah Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The monostate pattern is sometimes known as a conceptual singleton or the syntactic sugar over the singleton pattern.

It avoids all the issues that come with having only one instance of a class. Yet, all the instances share the same data. This is mostly performed through the use of static data members. One of the essential features is fully transparent to users, who have no idea they are dealing with a monostate. Users can build as many monostate instances as they wish, and each instance is equal in terms of data access.

Difference

Monostate Singleton
Having a monostate requires us to behave in a certain way (only one value along all class instances). A structural limitation is imposed by a singleton (only one instance).
The usage is transparent. The usage is not transparent.

Example

#include <iostream>
#include <string>
#include <unordered_map>
class Monostate {
public:
void addNumber(const std::string& na, int numb) {
teleBook[na] = numb;
}
void getEntries () {
for (auto ent: teleBook){
std::cout << ent.first << ": " << ent.second << '\n';
}
}
private:
static std::unordered_map<std::string, int> teleBook;
};
std::unordered_map<std::string, int> Monostate::teleBook{};
int main() {
std::cout << '\n';
Monostate tele1;
Monostate tele2;
tele1.addNumber("grimm", 123);
tele2.addNumber("huber", 456);
tele1.addNumber("smith", 789);
tele1.getEntries();
std::cout << '\n';
tele2.getEntries();
std::cout << '\n';
}
The monostate design pattern implementation

Explanation

  • Lines 9 to 11: We create a addNumber method to add the numb and na (name/key which refers to that number) to unordered_map.

  • Lines 13 to 17: We create a method named getEntries() that displays all the entries on the map.

  • Line 21: We create static unordered_map of the key-value (string and int) pair.

  • Lines 31 to 32: We create two Monostate objects.

  • Lines 33 to 35: We use the tele1.addNumbe() method to add data to the monostate set.

  • Lines 37 to 43: The getEntries() method displays the entries of the set. They both show the same entries because both point to a single set.

RELATED TAGS

singleton
monostate
design pattern

CONTRIBUTOR

Osamah Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring