• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singletons ???

 
Kavita Saluja
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one design question, which I am really confused about. The application that I am working on is really dynamic so the html is never coded in the jsp, its always build by the specific builders. For eg for different html types I have different html builders, for text i have a texthtmlbuilder class, for label i have a labelhtmlbuilder class, etc. All these builders generate the html based on one object 'Field' that is passed to them which has all the specifications. for eg this object called field will have getters etc for height, width, etc which the builders will be using to generate the html.

All the builders extend one base class which has implementation for methods that is common to all other builders. Every builder has a gethtml method which has its own specific implementation.

In the present implementation I am always creating a new object of the builder based on the type of the 'Field' object. No builder has any class level variables, everything is done based on this 'Field' object passed to the gethtml method of every builder.

The question is should these builders be singletons ? If yes, then all the gethtml methods I guess would need to be synchronized - wont this impact performance ?
 
C Chavan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since there are no member attributes in builder class, it should be thread-safe.

you may implement a builder factory (a singleton class) that will return an appropriate instance of base builder class based on the field type you pass to it. internally, you may cache either single or multiple instances of each of different type of builders and return to the caller.

public class BuilderFactory
{
private static HashMap _builders;
private static BuilderFactory _instance = new BuilderFactory();
private BuilderFactory(){ _builders = new HashMap(INIT_VALUE);}
public static BuilderFactory getInstance(){ return _instance;}
public Builder getBuilder(Field field){
if (field instanceof xxx){
if (_builders.contains(xxx)){
return _builders.get(xxx);
}
else{
Builder blder = new XXXBuilder(field);
_builders.put(xxx, blder);
return blder;
}
}
// repeat for other types of Field classes
:
:
}
}
 
Kavita Saluja
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I already have the implementation that you suggested (actually exactly the same).

My question here is should i change my implementation to make all those builders as singletons, since new objects are getting created (even though i maybe caching and pooling them) but still every object means more memory. So what I was thinking about is should I make these builders as singletons and make the gethtml method as synchronized for each builder. If I do so would it impact performance.

To put it another way - how to decide what class should be singleton (if the class doesnt have any class level variables or class is stateless).
 
rajeev tiwari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kavita,
I had similar business requirement at my end and the Framework, I am using GIS Framework, provides similar html builder (for htmltable, html form, table header, gridheader etc). To optimize the performance I played a bit with Framework and used getHtml method of these html builders as static (since these builder classes has no instace variable). Doing this, I did not need to make these methods as Singleton.

-regards
Rajeev
 
Kavita Saluja
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. Well if you made the classes static, that means the gethtml method must have been synchronized which I guess will effect performance.

As per your answer does it mean that if there are not any class level variables then that class classifies to be a singleton or static class ?

One more thing I am wondering about is whats the difference between a singleton and static class (logically and which one should be used where).

Regards
 
rajeev tiwari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kavita,
Well if you made the classes static, that means the gethtml method must have been synchronized which I guess will effect performance

Just by making getHtml method static will not make the method call synchronized but allow to work concurrently.

As per your answer does it mean that if there are not any class level variables then that class classifies to be a singleton or static class ?

No it is not the case. As the memory allocated is entirely different for static and non static classes. We can not use these things interchangably.

One more thing I am wondering about is whats the difference between a singleton and static class (logically and which one should be used where).

It will be good if we do not compare the functionality of static and singleton class. One having only one instance and other do not have instance at all (keep the master copy of its resources associated to class). If we are providing some functionality which is instance independant and do need to use heavily (e.g. custom utility classes of any project), I opt to go for Static method. But if some resource is to be needed to have one copy through out the life cycle of application (it is an instance), we go for Singleton class.

I hope it makes the explanations you looking for.

- Rajeev
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic