Enumeraton
The Multiton pattern is a quite an unknown design pattern for many developers. Probably because is not included in the classic Design Patterns book by the Gang of Four or maybe because it can also be seen as several singleton objects merged together.
The basis of this pattern is to have a set of objects where each can be accessed statically, like on the Singleton pattern, but providing an identifier (e.g. a string value) or a specific method for each one. Generally it is backed up by an internal map and objects are lazily initialized if the object is not found inside the map. In the Android SDK, for instance, they use this pattern under Context object. It provides a way to get a system service by providing the method getSystemService(String) with the name of the service. Here is a simple implementation of a Multiton in such way:
class DynamicMultiton {
private static Map map = new HashMap();
public Uri getUri(String service) {
Uri result = map.get(service);
if (result == null) {
result = Uri.parse("http://" + service + ".com");
map.put(service, result);
}
return result;
}
}
If we have a finite number of objects that are going to be allowed to be generated it is possible to have static constants that are accessed by named methods like it’s shown here:
class StaticMultiton {
private static Uri oosUri = null;
private static Uri googleUri = null;
public Uri get11870Uri() {
if (oosUri == null) {
oosUri = Uri.parse("http://11870.com");
}
return oosUri;
}
public Uri getGoogleUri() {
if (googleUri == null) {
googleUri = Uri.parse("http://google.com");
}
return googleUri;
}
}
This example can be understood as a compound singleton or a static multiton and can be useful, as an example, to provide the concrete instances of abstract factories.
With this post I am introducing a third implementation of this pattern, that simplifies its use in code and increases its performance when running. We part from the case of the dynamic multiton where a static map is used to store the objects. Well, in Java we have another way to implement key-value tables which is not commonly seen as such. These are the Enum types. Internally, a Java enumeration is a especial form of key-value table in which each entry is both the key and the value.
This is an example of how Enums can be used to load Android Animation instances with an Enum:
public enum Animations {
FadeIn(android.R.anim.fade_in),
FadeOut(android.R.anim.fade_out),
SlowFadeIn(R.anim.slow_fade_in),
LeftToRight(R.anim.animation_left_right),
RightToLeft(R.anim.animation_right_left),
LeftToRightOut(R.anim.animation_left_right_out),
RightToLeftOut(R.anim.animation_right_left_out),
Down(R.anim.animation_down),
Up(R.anim.animation_up);
private int resId;
private Animation animation;
private Animations(int resId) {
this.resId = resId;
}
public Animation get(Context context) {
if (animation == null) {
animation = AnimationUtils.loadAnimation(context, resId);
}
return animation;
}
}
The only problem with this implementation, as with static multiton implementations, is that you have to edit the enum to add new values, and therefore it’s less flexible than the dynamic multiton. Although with enumerations it’s rather trivial to add new values. On the other hand it has a very good advantage over dynamic implementations and this is that you save the lookup time for the key provided.
Tags: android, anumeraton, design patterns, enum, enumerations, java, multiton, singleton
No hay comentarios
Comments RSS
TrackBack Identifier URI
No comments. Be the first.
Deja un comentario

