Discussion on:

10
Comments

Join the conversation!

Follow via:
RSS
Email Alert
0 Votes
+ -
Contributr
... that's a lot of work! In VB.Net, you just mark a class or method with "Shared" and you are done. I understand why, Java gives you much more control doing it the way you describe, but you would think that since it is such a common pattern, they would have made it a bit easier.

J.Ja
0 Votes
+ -
Well I don't think that making common patterns a part of language is really a good idea. Better is to give more flexibility to developer. That's what all these Java patterns are about.
If you want maximum flexibility then program in Assembly. You can design the classes the way you want them. Adding common patterns to programming languages helps to speed up development. Personally, I think a programming language needs to be adapted to a specific field rather than making every language capable of doing the same thing. The idea that Cobol was written for business apps and Fortran for scientific/engineering apps allows a programmer to choose his design tool.
0 Votes
+ -
...that amount of work is necessary in VB.NET too... VB's 'Shared' is analogous to java or c#'s 'static'. Marking a class Shared or static makes it non-instantiable, like a module, not a Singleton. The pattern for Singleton classes is exactly the same in VB.NET as it is in Java as it is in C# as it is in C++, etc... (save for the thread-safe stuff). No language has direct support for this pattern, because it IS so easy to implement with standard language constructs.
0 Votes
+ -
Contributr
I am truly sorry, I goofed big time! I was not really paying best attention, and thought that was all to prevent instantiation! My deepest apologies!

J.Ja
Although it has valid uses, i think Singleton is one pattern that has been consistently (ab)used, especially in Java code. It's too easy to make a class a Singleton and end up with code that's very tightly coupled, is difficult to unit-test as it's difficult to mock, and makes a lot of assumptions about the calling code. Have a look at this:

http://www.ibm.com/developerworks/webservices/library/co-single.html
If you need to make the class Serializable, you need to add a readResolve() method to the class:

public Object readResolve() {
return getInstance();
}

Otherwise, deserialization will construct a new instance of the class and use it. The pre-enum style of type-safe enumerations has the same problem.
This article seems woefully incomplete when talking about multi-threaded access to Singletons in Java. See this for a more thorough discussion.

You may also be interested in this discussion of why you should avoid using Singleton in the first place.
In my opinion, every article about singleton should start with warning "try avoid using it" with suggestions to overcome the problem. Any time we had singleton we discovered a number of bugs, and once we got the requirement to support more than one instance, re-factoring was a nightmare. One simple rule to remember - one instance of object doesn't mean singleton pattern.
0 Votes
+ -
Singleton Enum
javabuddy 16th Feb 2011
Hi ,

There could be another solution if Singleton is written in JAVA by using Enum way as explained in book "Effective Java".

public enum Singleton {
INSTANCE;
}

Thanks
Javin
Why String is immutable in Java
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.