tags:

views:

143

answers:

5

I have a Blog class that does exactly what you think it would...create an object and handle it. That part is nice and clean.

I am now in need of a function that will quickly return a number of all the blogs in my database (not necessarily related to the blog object). Therefore, I am thinking a static method would be a good choice.

My question is, where should I store this static method? Here are a few options I can think of:

  • store it as a static method in the Blog class (maybe smelly because it has nothing to do with the object that class creates?)

  • create a new class for blog static functions (seems excessive)

  • find a better way to go about this altogether (yes, but what?)

+1  A: 

You could also encapsulate the database access in a class and add the new method there.

Erich Douglass
+2  A: 

Create a class/interface called BlogService which will have count method in it. Other methods such as findAll, findById, etc.

Chandra Patni
so this would keep it from being static?
johnnietheblack
Yes. static members make code less object oriented and hard to unit test. For instance, static methods can't be overriden.
Chandra Patni
I like this solution, but not due to the nature of statics. The way I generally code is such that I have a 'Service' class that manages multiple models of the same kind, and takes care of operations on groups of models, so that my model classes represent a single business object or entity. I find that doing it this way, allows me to easily abstract away all of the 'manangement' of the objects as a group, and leave the model dealing with only a single business object.
JC
+1  A: 

Presumably something is managing the multiple Blog objects you create? If not, there should be, and that's where the method belongs - no need for it to be static.

anon
indeed...my controller (MVC) manages it...but i will be reusing the need for this function on several pages...from what i've read, its better to localize this funcationality, right?
johnnietheblack
A controller in an MVC solution (and I personally think MVC is pants) should have no business level knowledge - it should use a business level object to get the work done, and that business level object is where your function should reside.
anon
aka, the model? sorry...if its not clear...i am definitely stepping into a new world for me
johnnietheblack
and, if thats the case...i guess if i am creating a business level object, that is kind of what im asking. do i put it in the Blog object, or create a new one?
johnnietheblack
Once again,the model in an MVC solution should refer to business level objects. This means you can use the business level objects in in non-MVC designs. I haye to suggest the dreaded "manager" type of object, but what you seem to need is a "BlogManager" class.
anon
okay i think i get it...i appreciate the patience, neil...that seems to be the most inline with what im already doing and the most reusable.
johnnietheblack
A: 

find a better way to go about this altogether (yes, but what?)

Model View Controller

Transform your Blog to a 'Controller' at this stage

Create a 'Model' that can handle all your database methods

From Class Blog call the Model as you wish (also suggest at this point refactor all database requests to go into Model).

(For a very good Introduction to MVC search for CodeIgniter).

yannis
A: 

You do not need static method, but static variable. Static method is just function in object that does not take object but only variables from outside.

ralu