This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
In an object-relational mapping model, one would have an instance of an object mapped to a row in a relational table. To manage a collection of instances, a separate object manager instance could be created to: get a list of instances, save a list of instances, etc. An alternative would be to use static methods at the instance's class level to perform the same management functions. What are the advantages/disadvantages to each approach? It seems like static methods would cut down on object clutter, but do they perform well in a high volume, multi-user environment? Is there a potential bottleneck issue with multiple users accessing the same static methods?
Static methods have some issues with flexibility. When you say SomeClass.method() you're committed to SomeClass. We can't easily slip you a subclass or another implementation of an interface for custom behavior. If you say
then the factory can return a list manager for databases or one for XML web services, or one for flat files, or one that works all in memory for unit testing.
And, yes, static methods that use static variables can have thread issues. If they only use local variables they can be made safe ... or not.
Performance is generally not a consideration in deciding when to use static methods. Other design factors, like the flexibility shown above, are much more important.
Hope that helps!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi