Jump to content
Posted 05 September 2000 - 09:41 AM
Posted 05 September 2000 - 10:27 AM
Posted 06 September 2000 - 01:13 AM
Posted 06 September 2000 - 03:01 AM
Posted 06 September 2000 - 03:24 AM
Posted 06 September 2000 - 05:32 AM
Posted 06 September 2000 - 07:54 AM
Original post by felonius
The singleton class solves the general problem of making sure that only one instance is available of a class and that it always is available.
This does not solve problem at hand which is that MyClass has multiple instances that must use some of the same data.
Singleton variables solve another problem.
You cannot say that singletons are more OO than class variables. That would be like comparing apples and bananas. And even if we could compare them class variables are very central to OO. In the original OO languages like Simula or SmallTalk class variables played a very central part.
In fact in your sample you still use class variables. Writing
private static MyClass instance = new MyClass();
is not different from:
private static MyClass instance;
instance = new MyClass();
The only thing that has happened compared to the code I suggested is that you have encapsulated the array in the class and thereby preventing you from adding other data members to MyClass.
Posted 06 September 2000 - 10:27 AM
But it''s "bad" to expose class variables to the outside world. You should only provide access to class variables via method calls, and class variables should be private, except for constant values.
That way you can alter the functionality and/or contents of the class, and as long as you don''t change the methods, you don''t break any code that USES the class. You also enforce the black-box nature of the class, because clients of the class neither know nor care about the inner workings of the class, just about what the interface to the class is (the methods).
Posted 06 September 2000 - 04:02 PM
Posted 06 September 2000 - 05:58 PM
Posted 10 September 2000 - 08:30 PM
Posted 14 September 2000 - 05:33 PM
Posted 14 September 2000 - 11:19 PM
Posted 15 September 2000 - 01:03 AM