Reputation: 4606
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?
Upvotes: 3
Views: 6994
Reputation: 101
Just adding to the answers,
I am creating a game called OTHello from Cracking The Coding Interview book, Where there will be only two players, where one is white player and other is black player.
So i have a class Player which should only have two instance per game. Each instance should have either black or white. After both players are created the board which is like the class that holds all players,coins,points,...ETC should not have any more players interrupting so this pattern will object that from happening.
The same can be applied for chess also where there can be only two instances of player class in one instance of board class.
Upvotes: 0
Reputation: 2923
Another example:
Assume you have some surveillance cameras and each camera can have one and only one controller.
In this case, you should get the camera class multiton. it has a hash map that contains <key, camera>
pairs. like:
public sealed class Camera
{
static Dictionary<int, Camera> _cameras = new Dictionary<int, Camera>();
static object _lock = new object();
private Camera()
{
HardwareId = Guid.NewGuid();
}
public static Camera GetCamera(int cameraCode)
{
lock (_lock)
{
if (!_cameras.ContainsKey(cameraCode)) _cameras.Add(cameraCode, new Camera());
}
return _cameras[cameraCode];
}
public Guid HardwareId { get; private set; }
}
Upvotes: 1
Reputation: 19260
Multiton
is a design pattern which will ensure only one object will be created for a Key in multi threaded environment. So if multiple threads try passing same key should get same object(lock object) for that key. May be we call it as key based singleton.
If we have ONLY ONE lock object in overall system; then it is singleTon
for your application. But here multiple lock objects; and each object mapped with a key.
One example is; assume there are multiple conferences and you want to allow callers of a conference should be joined one by one (to count number of callers in the conference) (SYNCHRONIZED PER CONFERENCE OBJECT) with their respective conferenceID. If i have singleton object then even the callers from different conference will be blocked. So i need lock for each conference.
So Conference lock objects should be created based on conference id; and when multiple threads trying to access same conference object with the same conference id (multiton key) should end up with synchronization in the system. So that if two callers dials of a same conference at the same time will be SYNCHRONIZED.
class LockByKey {
ObjectForStringKey objHolder = new ObjectForStringKey(100);
public void lockThenWorkForKey (String key) {
synchronized(objHolder.valueOf(key)){
//DoSomeWork
}
}
}
//MultiTon
public final class ObjectForStringKey {
private final Object[] cache;
private final int cacheSize;
final int mask;
public ObjectForStringKey(int size) {
// Find power-of-two sizes best matching arguments
int ssize = 1;
while (ssize < size) {
ssize <<= 1;
}
mask = ssize - 1;
cache = new Object[ssize];
cacheSize = ssize;
//build the Cache
for (int i = 0; i < cacheSize; i++) {
this.cache[i] = new Object();
}
}
public Object valueOf(String key) {
int index = key.hashCode();
return cache[index & mask];
}
}
Upvotes: 3
Reputation: 2062
I think the example of Java's ScriptEngine
is a good example:
// For nashorn
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
// For rhino
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
Nashorn and rhino are both (generic) ScriptEngine
's, only the parameter shows which implementation is used.
Upvotes: 0
Reputation: 21657
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
this is multitone, a real life example
Upvotes: 5