• Post Reply Bookmark Topic Watch Topic
  • New Topic

using static methods  RSS feed

 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
I am facing an issue which I dont understand

Using struts - and I understand that struts implement the "VC" of "MVC"
So from my action classes need to communicate with the database

As an example - user is logging into the application
The action class is - LogonSubmitAction

This action class needs to get data from database and authenticate user

To decouple the Model from struts framework - used simple plain java class as an intermediate layer .
This talks between struts and DAO

This layer ( simple Java class with methods ) makes call to DAO - which in turn communicates with the database .

Here is my question :
1 >
To avoid creation of DAO objects - I made all the methods static in the DAO

2 >
As an OO principle - accepted this is not good but would this cause
any performance hits ?

3 >
Where are the static methods stored by application server ?

4 >
If I create too many static methods - will this be an issue ?

5 >
Are static methods ever garbage collected ?

6 >
In case that the method in DAO takes a long time to execute ( connecting to database fetching data etc ) - would other invocations of the same method
experience a performance hit - i.e similar to a lock on the method and it not being accessible to other callers ?

If someone could answer my doubts it would be most helpful.
Regards ,
-anagha
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, comrade

I believe that most of your questions do not have to do exactly with Java Web Programming, so I will reply them although my experience with Java Web Programming is limited.

Question 1:

To avoid creation of DAO objects - I made all the methods static in the DAO

Reply
I do not believe there is anything wrong with this, actually there is pattern known as Static Factory Method. Joshua Bloch, senior staff engineer from Sun Microsystems talks about it in his book: Effective Java Programing.

However, you might consider inheritance issues related to this approach since static methods do not inherit it could be issue if you pretend to extend your DAO objects in the future (I believe, in practice, when it comes to DAO objects, this rarely happen)

You can read an article about it in JavaWorld

Question 2:

As an OO principle - accepted this is not good but would this cause
any performance hits?


Reply
Your DAO Factory will not be loaded until it is actually needed. That would be the first time you call one of your static factory methods. Hence, you will save the time it takes to initialized a new Object since your DAO Factories just contain static methods and you will never create instances of your DAO objects.

If you need to provide some initilization code to your DAO (i.e. you need to provide a Connection DataSource), instead of using simply factory methods, you could transform your DAO into Singletons (GoF), by means of making the constructor private and providing a static getIntance() method that can initialize the the Object.

Still you might face inheritance issues directly related to Singleton as explained by GoF.

When it comes to executing the code in the method there is no difference between static and non-static methods.

Question 3:

Where are the static methods stored by application server ?


Reply:
Static methods are not stored anywhere. They belong to tha class, and they are available as soon as the class is loaded. The JVM will load the class the firt time that you use it (i.e. use a static member of tha class).

Classes are loaded by the JVM into memory using the application server classloader. Depending on the implementation of the server it is probable that every container used by the server has a proper classloader wich will be responsible of loading your classes into memory.

Question 4:
If I create too many static methods - will this be an issue ?

Reply:
Basically, you are compromising inheritance of your of your Factories. Hence, it is an issue if you pretend to extend your classes. Beyond that you can have as many static methods as you want.

However, general programming principles might alert about badly implemented code. If your class has too many methods you may evaluate if all responsabilities executed by the methods really belong to the class. If you feel the class is carrying out resposabilities not related to it, then you can refactor some code into another class. It is the same principle if your mehtods are to long, then maybe you can refactor some code into another methods.

You may like to give a look to the Martin Fowler's book about Refactoring.

Question 5:
Are static methods ever garbage collected ?

Reply:
Only objects are garbage collected. As your class is only composed by static methods there is nothing to collect.

However there is class object related to your class. This will be destroyed by the garbage collector only until the classloader that loaded it has been garbage collected. This is unlikely to happen during the existance of your Web Application unless the application server is stopped.

Question 6:

In case that the method in DAO takes a long time to execute ( connecting to database fetching data etc ) - would other invocations of the same method
experience a performance hit - i.e similar to a lock on the method and it not being accessible to other callers ?


Reply:
Unless you are synchronizing your methods, they must be always available to all callers. That�s to say, two different threads may be invoking the same method concurrently.

The administration of the resources your method uses is another story. For instance, if your are using only one database connection for all threads. In this case all requests to the database through this connection will be put in a queue, if the first thread asks something from the database, the second thread request will be put in a queue until the first request is solved. In this case, you may consider using a connection pool.

But this is a different issue.

Regards,
Edwin Dalorzo
[ April 05, 2006: Message edited by: Edwin Dalorzo ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The biggest problems with static methods are hardcoded dependencies and lack of flexibility. If you ever wanted to have polymorphic versions of DAOs you'd be out of luck.

Why would that happen? In my work framework we have DAOs for UDB, Oracle, MQ-Series and partner web services, plus the option to hand-code custom ones. But even more important, the Test Driven Development style would quickly make you want mock DAOs or a memory-only datastore so you can test without setting up and tearing down a real database.

Rather than code a lot of static methods, I'd code an interface and a class that is a factory for a Singleton instance of itself. Later on if you need alternate DAOs you can mod the class to return instances of some implementation of the interface.

If you've already coded a ton of calls to static methods, all is not lost. Make all those static methods do nothing but call identical methods on a private instance of a real DAO. Give yourself a reference to the real DAO through a factory or better yet dependency injection.

Let us know if that raises more questions than it answers.
[ April 05, 2006: Message edited by: Stan James ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anagha, can you tell us why you want to avoid the creation of DAO objects?
 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
I was thinking that very rarely ( if ever ) would I need to want to subclass a specific DAO class.

So was thinking - why do I want to create an object ( from my standpoint - creating an object seems unnecessary and expensive at the cost of compromising OO principles where I lose inheritance ) - when I could simply use a static method .

Hence I was planning / and have used static methods in DAO.

Please let me know if this is bad / poor design .

And yes - thanks to everyone for giving their inputs .

Regards ,
-anagha
[ April 05, 2006: Message edited by: anagha patankar ]
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anagha patankar:
I was thinking that very rarely ( if ever ) would I need to want to subclass a specific DAO class.

Generics added parametric polymorphism which should make it easier to leverage commonalities in DAOs. Generic Data Access Objects
[ April 06, 2006: Message edited by: Peer Reynders ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What seems expensive about creating an object?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!