Regards
Azrael Noor
Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
what do the methods do? Estimating performance without knowing what the methods do is difficult
Here's a similar question: I'm thinking of a thing. Is it big?
Plus, they make good unit testing much harder.
Regards
Azrael Noor
Jesper de Jong wrote:If you're using a lot of static methods, then you are not making use of object oriented programming. Your program will then essentially be a procedural style program, just like an old C program with only global functions.
Using static methods and singletons makes unit testing much harder because it's almost impossible to swap out components with mock components for unit testing. Suppose, for example, that you have a class named PersonDao which is a data access object to store and retrieve Person objects from a database. And suppose that you have some component in your system that uses PersonDao.
For testing that component in a unit test, you'll probably want the component to not use the real PersonDao that writes to and reads from the database, but you want to use a mock version of PersonDao, just to check if the component to test calls it properly.
If PersonDao would contain only static methods, it would be almost impossible to replace it with a mock version for testing. Your code that uses PersonDao would look like this:
What if you would want to use a MockPersonDao instead of the real PersonDao for testing? That's impossible without changing the code above.
To make the code more testable, it would be better to create a PersonDao interface, and then create a PersonDaoImpl class with the real implementation, with non-static methods. For testing you can create a MockPersonDaoImpl which also implements the interface. You could then use dependency injection to pass the appropriate PersonDao implementation to the component. For testing, you'd pass it the MockPersonDaoImpl, and for "real", the normal PersonDaoImpl.
Regards
Azrael Noor
Azrael Noor wrote:3) Using Lot of Static Methods, can create issue in Multithreading (<---- Confuse with this, in case of static methods, in case of static instance variables it would be difficult as multiple thread use same variable so value collision, but in case of sttaic methods i think we are passing different parameters everytime so it will calculatre different results)
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|