Local database reading edition

Advantage of singleton over static methods?

Reconstructed from database records, not a captured original forum layout. Dates and display names are retained as recorded; timezone and real-world identity are not inferred. Historical advice, contact details and offers are not current. Original markup is shown as inert text.

Forum index · Object Oriented Design

Opening post

Asha · 2001-02-15 12:49:32

I have seen singleton classes used in many places.But most of the programmers has the question of why can't not i use static methods ,what is the deal of using Singleton? Here is my thought. The difference between using a singleton over a class with static methods boils down to effective object-oriented design. Singletons normally represent a cleaner approach. A class of static methods, unfortunately, breaks down to a simple list of functions. You may ask, what's the problem with a list of functions? With a list of functions you no longer perform object-oriented programming. In fact, your work easily devolves into a classic procedural program. Suddenly, objects no longer represent the focus of your program. You begin to fall into a data-centric programming mode. That is, instead of sending messages to objects that encapsulate state and behavior, you begin to call functions that act on data. Asha Balasubramanyan http://www.ammu.org

Reply 20

Padma · 2001-04-20 20:20:15

Addition to Asha's facts, A class with all static methods and variables can use encapsulation, but will not benefit from inheritance or polymorphism. In java, only non-static variables or methods can be inherited, and interfaces can contain only non-static methods, so to gain the power of inheritance and polymorphism, we have to use non-static methods. So if all our methods are static, we've lost two of the three motivators for going OO. But worst of all, if all methods in a class are static, all non-local variables will also have to be static, which means they are shared, which means that two objects of the same class cannot have different persistent states. It would make more sense to apply the singleton pattern to a class with non-static methods rather than declare all methods static. Padma

Source notes

MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 34, 20