Sunday, March 6, 2011

Classes that have no member data

What do you do with classes that have no member data, only methods?

Do you make them static?

In my case it is an repository class that executes queries against the database. Maybe I got the repository pattern wrong... (It does implement an interface)

From stackoverflow
  • It depends. Most of the time it might be possible to make the class static, but sometimes you need to pass an instance of it around. Sounds like you might be having this situation. In this case perhaps you should consider a Singleton pattern, since more than 1 instance of the class is not likely to be needed?

  • If it implements an interface, and gets passed around as that interface, then you can't make the members (or the class) static. The interface aspect means that although an instance won't have any actual fields, it still contains valuable information - its type.

    You might want to make it a singleton, but there's no particular need to.

  • Inherit from an Interface mean that you cannot use static. Simply create a class and instantiate it.

  • Why won't you make a database wrapper class, that maintains the connection to database opened/closed. Moreover, it can also open it automatically if new query is sent. You can include the functions your class has, running them right on the inner pointer to the database.

    I guess this is the best database management pattern. If you use it, you should make a Factory method that returns the object of this class initialized on some specific database. Then you pass that object around.

    Or if you are lazy and sure that you will need only 1 database, make it a singleton.

0 comments:

Post a Comment