From: Stewart T. <wsr...@ho...> - 2005-03-04 18:13:12
|
Erik, >2. StockMarket is a singleton and should IMO be constructed as such. >This means: private constructors, access only via a static get() >method that returns the one instance (which is created the first time, >or from a static init block). >In addition to safety, this removes the need to pass around object >variables, >as each class needing access to the stockmarket object can get it >immediately by calling StockMarket.get(). Please think very carefully that you understand the implications of making the constructor "private" in Java when creating a singleton class. A class with a private constructor is implicitly final and cannot be extended with a subclass. As long as you want to prohibit subclasses of your singleton, then this is fine. However, if there is a possibility that it might be useful at some point for someone to create a subclass, then it is better to make the constructor "protected". Please note, I'm not suggeesting that StockMarket should not be a singleton, I'm simply making an observation about Java style and behaviour. You're going to be busy this weekend... have fun... I'm off skiing, so I'll b ehaving fun too! cheers Stewart |