Questions tagged [multiton]

The Multiton pattern is a creational design pattern. This pattern expands on the singleton concept to manage a fixed number of instances of the Multiton class. This typically done with a map of named instances as key-value pairs.

The Multiton pattern is a design pattern similar to the singleton, which allows only one instance of a class to be created. The Multiton pattern expands on the singleton concept to manage a fixed number of instances. This typically done with a map of named instances as key-value pairs.

Rather than having a single instance per application (e.g. the application object itself in many programming languages) the Multiton pattern provided a fixed set of instances.

A simple example for a Multiton class may be the class Weekday, which allows just seven instances named monday, tuesday, wednesday, thursday, friday, saturday, and sunday. This class keeps its constructor private, and allows access to the seven instances by one class method (e.g. weekdayByName()) or seven class methods (one for each weekday).

Reference wikipedia

30 questions
13
votes
5 answers

Thread safe multitons in Java

Given the following multiton: public class Multiton { private static final Multiton[] instances = new Multiton[...]; private Multiton(...) { //... } public static Multiton getInstance(int which) { …
donnyton
  • 5,874
  • 9
  • 42
  • 60
7
votes
1 answer

Annotating a Java class as safe for reference comparison

I have a class that's a multiton, so I know that given a particular key, there will never be two instances of the same class that exist. This means that, instead of: if (someObject.equals(anotherObject)) ...it's safe for me to do this: if…
user3466413
  • 805
  • 1
  • 7
  • 16
5
votes
2 answers

Instance-controlled classes and multithreading

In Effective Java Chapter 2, Item 1 Bloch suggests to consider static factory methods instead of constructors to initalize an object. On of the benefits he mentions is that this pattern allows classes to return the same object from repeated…
thee
  • 510
  • 1
  • 7
  • 20
3
votes
5 answers

Multiton - real world example?

I'm currently reviewing design patterns. I came across this one Multiton, but I find it difficult to think of a good real-worlds usage example. So what's the main field of application for the strengths of Multiton pattern?
karla
  • 4,506
  • 5
  • 34
  • 39
3
votes
3 answers

How to create a class that doesn't re-create an object with identical input parameters

I am trying to create a class that doesn't re-create an object with the same input parameters. When I try to instantiate a class with the same parameters that were used to create an already-existing object, I just want my new class to return a…
Paul
  • 42,322
  • 15
  • 106
  • 123
3
votes
3 answers

Elegant and 'correct' multiton implementation in Objective C?

Would you call this implementation of a multiton in objective-c 'elegant'? I have programmatically 'disallowed' use of alloc and allocWithZone: because the decision to allocate or not allocate memory needs to be done based on a key. I know for sure…
ArjunShankar
  • 23,020
  • 5
  • 61
  • 83
3
votes
3 answers

C++ templated class implementation of the multiton pattern

I implemented the multiton pattern using a templated class in C++. #ifndef MULTITON_H #define MULTITON_H #include template class Multiton { public: static void destroy() { for (typename…
user282519
2
votes
2 answers

"Multiton" implementation of frozensets - just one instance per value

How can I implement the Multiton design pattern for frozensets, in a way which works no matter how the frozenset is created? What I'm looking for is class that behaves just like frozenset, but that gaurantees "full interning": for any two instances,…
Just Me
  • 413
  • 3
  • 9
2
votes
1 answer

Singleton (or Multiton) Pattern Based on Elapsed Time

Below I've implemented the Multiton pattern but I think this question applies equally to the Singleton pattern: private static ConcurrentHashMap instances = new ConcurrentHashMap(); public static LocatorDAO…
Kevin Little
  • 263
  • 1
  • 4
  • 10
2
votes
4 answers

.NET - limiting the number of instances of an execution unit

Let's say I have an application written in C# called EquipCtrl.exe which runs as a local process on a PC to control a piece of equipment. Obviously, I would wish to have only one instance of Equipctrl to run on each PC. If I had two equip to control…
Blessed Geek
  • 21,058
  • 23
  • 106
  • 176
2
votes
2 answers

Singleton Pattern: Multiton?

I am confused as to how the multiton implementation of the singleton pattern works. I am aware that the definition of a singleton is as follows: Ensure a class allows only one object to be created, providing a single point of access to…
user3287264
2
votes
3 answers

Thread-safe multiton pattern

Inspired by a comment to an given answer I tried to create a thread-safe implementation of the multiton pattern, which relies on unique keys and performs locks on them (I have the idea from JB Nizet's answer on this question). Question Is the…
mike
  • 4,929
  • 4
  • 40
  • 80
1
vote
1 answer

WeakMultiton: ensuring there's only one object for a specific database row

In my application I need to ensure that for an entity representing a data row in a database I have at most one java object representing it. Ensuring that they are equals() is not enough, since I could get caught by coherency problems. So basically…
lultimouomo
  • 248
  • 1
  • 2
  • 6
1
vote
1 answer

Multiton pattern in C++ failed with C2676 under MSVC Compiler

I am writing a mutiton pattern class in C++, but run into C2676 with MSVC compiler. C2676 : binary '<' : 'type*' does not define this operator or a conversion to a type acceptable to the predefined operator. Here is my code: #include…
EV0R
  • 13
  • 2
1
vote
3 answers

Enum Multiton Pattern

Is a good idea to use enum's for Multiton pattern if the number of instances for Multiton is fixed at compile-time.I have seen the Enum Sigleton pattern,so i was just wondering if the similar can be done for Multiton's too ?
Emil
  • 13,577
  • 18
  • 69
  • 108
1
2