aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Helper - Singleton or Static? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Helper - Singleton or Static?" Watch "Helper - Singleton or Static?" New topic
Author

Helper - Singleton or Static?

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Hello Friends

Just provoking a discussion for deciding whether a helper class for me should be a Singleton or Static or a normal?

I have a service class for creating audit database entries. And all these audit db entries will be created by the helper class. Auditing is a side effect of a main transaction. Not the main use case.

Should this class be SINGLTON or NORMAL class or a STATIC class with all methods static

Class Contents and Issues to note:
data = Has 3 HashSets (to filter and hold temp data required during creation of audit logs
methods = Has methods to create audit entries using 3 different hash sets

My concern:
Singleton - Creates only a single object within a class loader. (Issues like double locked checking exists) . Less memory footprint but not sure how these 3 hash sets will be working as Hash sets are not synchronised how to deal with that). Here class will use 3 hash sets for handling calls from all the clients. Might cause a performance delay or concurrent modification error during iterations.

Static - Has every methods static but requires temp variables which will be modified by calls from service layer (Hash sets are not synchronised how to deal with that, concurrent modification )- less memory usuage as there is no real class instance created. So all methods will use the globally declared 3 static variables.

Normal class - Will create many objects in the jvm and each object will have 3 different hash sets for each calls for creating audit entries. (high memory)

So what will be the approach taken by you in this instance?

Apologize if anything is missing in terms of detail, Would really appreciate if you can kindly post your thaughts?

SCJP, SCWCD, SCBCD, SCEA 5
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597

Are you going to use it in clustered environment?

I assume you are using it in multi-threaded environment.


Sandeep
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Hello Friend
Thanks for your reply, Yes I am using in a clustered environment
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
If there is any particular reason you are using Set, then try ConncurrentSkipListSet. I have never used this class.

If you are ok with not using Set, then there are many collections in java.util.concurrent package which will do better job in multi-threaded environments.

If you are not using jdk 1.5 and above, then you can get synchronized version of HashSet,Collections.synchronizedSet.

Or write your own synchronization code.

I will avoid using SingleTon in clustered enviorment because you will have to rely on your application server for SingleTon Service. So I will go with your no. 2 option, static implementation.

Hope this helps.
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
Or may be I will also try 3 option if it is really not taking too much memory.
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Thanks Sandeep

Can you please explain why you would go for point 2 ? I dont want to use Collections.synchronised due to performance implication of delay happening due to Synchronisation?

Guys can I have you take on it please?


Thanks
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
Mohamed Farouk wrote:

Can you please explain why you would go for point 2 ?

Can you explain why you do not want to go with 2 or 3 with java.util.concurrent package when you have clustered environement?

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Can you explain why you do not want to go with 2 or 3 with java.util.concurrent package when you have clustered environement?

I dont want to use Collections.synchronised due to performance implication of delay happening due to Synchronisation?
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
You do not have to use synchronization if you use classes in concurrent package, only methods like size() may not give you exact size. Go through API documentation. Always remember a rule. If something is given to you by API or framework, do not try to reinvent the wheels.
Best Luck.
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
Following is copied from API documentation of ConcurrentSkipListSet.

Beware that, unlike in most collections, the size method is not a constant-time operation. Because of the asynchronous nature of these sets, determining the current number of elements requires a traversal of the elements. Additionally, the bulk operations addAll, removeAll, retainAll, and containsAll are not guaranteed to be performed atomically. For example, an iterator operating concurrently with an addAll operation might view only some of the added elements.

This class and its iterators implement all of the optional methods of the Set and Iterator interfaces. Like most other concurrent collection implementations, this class does not permit the use of null elements, because null arguments and return values cannot be reliably distinguished from the absence of elements.


If you are not okay with this, then write your own synchronization.

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Iterators are weakly consistent, returning elements reflecting the state of the set at some point at or since the creation of the iterator. They do not throw ConcurrentModificationException,


Many thanks sandeep for this good reply. Now i was not very clear of what "weakly consistent mean" , can youprovide your explanation. i use iterators on this collection and what do you think is the implications.

What do you think the cons of using the same ConcurrentSkipListSet along with a Singleton instead of a static helper methods all over the class?

Regards


Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
I think you have already replied to this question in earlier post sorry for asking again:

I will avoid using SingleTon in clustered enviorment because you will have to rely on your application server for SingleTon Service. So I will go with your no. 2 option, static implementation.

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
I am using 1.4 jdk is there any implications of using edu.emory.mathcs.backport.java.util.concurrent.ConcurrentSkipListSet of backport-util-concurrent-3.0.jar?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Helper - Singleton or Static?