On Wed, 23 Feb 2005 10:36:40 +0100, Giuseppe Greco <[EMAIL PROTECTED]> wrote: > On Tue, 2005-02-22 at 09:41 +0800, Troy Laurin wrote: > > > [DCL] > Would you suggest something like this? > > sealed class ResourceUtils > { > private ResourceUtils() {} > public static readonly ResourceUtils Instance = new ResourceUtils(); > ... > } > > ... > > string errorMessage = ResourceUtils.Instance.GetString("NA1001"); > > That works fine too, but looking at > http://msdn.microsoft.com/library/default.asp?url=/library/en- > us/dnbda/html/singletondespatt.asp, on .NET the result is > exactly the same as with the double-checked locking pattern
That is exactly what I would suggest. Note that the result is exactly the same as with double-checked locking, but the implementation is very different: Using DCL, you have to guard against multiple threads attempting to construct your object at about the same time, and you need a memory barrier (or a volatile field) to ensure that the object created by the first thread is visible to the others. Using inline initialisation, you are guaranteeing that the object will be completely constructed before anyone tries to access it, and a side-effect of this guarantee means you no longer need memory barriers to effect visibility. Or rather, the memory barrier you have to pass to access the class definition (exactly once, the first time you access the class) also suffices for the member. > Oh no! Java? Pfui!!! I come from C/C++... and my bibles were > "Effective C++" and "More Effective C++" by Scott Meyers... There's no need to get that way, if you believe in good programming as well as effective programming. There's a lot of (worthwhile) things that you can do in C++ that are impossible (or at least don't make sense) in Java, but not all that many projects need to do that kind of thing. Anyway, there's no reason why a Java programmer can't understand what's going on beneath the covers, too :-) I think of myself as a Java programmer, but I cut my teeth on C. (Actually, I cut my teeth on Basic, then moved onto Pascal at school, then worked in C after that... but you get my point) > > j3d. Peace, -- Troy ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ nant-developers mailing list nant-developers@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/nant-developers