• Post Reply Bookmark Topic Watch Topic
  • New Topic

Business Logic in Static methods or instance methods  RSS feed

 
K Abhijit
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody...

in J2ee paradigm, specially in EJBs, we see the business logic get wrapped in some helper/worker classes...more in case of stateless EJBs
In short EJB layer gets control from client, the Value object is been populated..it is been passed over to some static methods of the worker/helper class and work is done..


Most of the time, the same thing can also achieved using the instance methods with instance variables.

there are couple of supportive arguments made for static method usage are like
1. keeping the values objects passed as parameters and rest keeping as local variable you need reduce the concurrency issues
so need not worry about thread safety..
2. static methods get invoked faster with respect to instance method specially when we're using runtime polymorphism ( i'm bit unsure for the actual performance gain though technically speaking we do have a edge for static invocations over polymorphic invocations) ...

in favor of Instance methods approach it is said"
if you are passing any parameter as argument to most of the methods (static) then that parameter becomes a strong candidate for being instance member of that class...so better go for instance method with instance field.

could anyone advise getting
1. actual pros and cons of these 2 approaches?
2. when should we use static methods and when should we go for instance methods(with instance variables of course) for putting business logic
3. what all performance dynamics we have for these 2 approaches
4. any approach which is better for longer term maintenance...

thanks in advance..
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K Abhijit wrote:1. keeping the values objects passed as parameters and rest keeping as local variable you need reduce the concurrency issues
so need not worry about thread safety..

That is not true. Is doesn't make any difference if you're getting a reference from an instance of as a parameter when talking about thread safety.

K Abhijit wrote:2. static methods get invoked faster with respect to instance method specially when we're using runtime polymorphism ( i'm bit unsure for the actual performance gain though technically speaking we do have a edge for static invocations over polymorphic invocations) ...

That performance gain is almost nothing. And even if there where a small performance gain then you should not use static because of that. Premature optimization is the root of all evil. If there are performance issues then you should profile your application and find the trouble makers.

And when using static methods you lose the polymorphism ability. Abandoning that ability will significantly increase the complexity of your code and will be much harder to maintain.
 
K Abhijit
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Oet.
Oet wrote:That is not true. Is doesn't make any difference if you're getting a reference from an instance of as a parameter when talking about thread safety.

completely Agreed ... No Thread safety for references which have been passed as parameter...
My mistake I think i was not able put my point correctly, I was referring more for local variables when i referred thread safety

I was just explaining one of the way logic gets coded in for middle tier (Stateless beans).. something like this..

some how it resembles me procedural approach of coding...

Also could not agree more with your last point that static approach kills you polymorphism capabilities.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!