Usually JavaPractices.com is a good site with good idea's, but this one troubles me: JavaBeans are bad.
The article cites several reasons, mainly that the term JavaBean means "A Java Bean is a reusable software component that can be manipulated visually in a builder tool." not Data storage, violates certain patters, and is more complex.
Now I can agree with the last one, but in my eyes JavaBeans in a list makes a lot more sense than nested Maps. The article claims that database mapping frameworks should call constructors, not set* methods, and the object should be immutable. In my mind however, calling set* methods when trying to build an object is easier to read than new MappedObject("column1", "column2", "yet another column", "this is stupid");
I also use the JavaBean style class for other things besides database mapping, eg for an IRC bot, having an object per user that gets updated with various things. I don't want to create a new object every time new information is given, I want to add it to an existing one.
So my question: Is using JavaBeans for data storage a bad practice and should be avoided, or is it perfectly safe?
It seems that you are misreading the text.
Now I can agree with the last one, but in my eye's JavaBeans in a list makes alot more sense than nested Maps
The text never mentions nested maps as an alternative ( yiack )
...should call constructors, not set* methods, and the object should be immutable
This is a good practice, specially useful when dealing with threads.
But we can't say that using setters is baaad either, specially when a single thread is using the object. That's perfectly safe.
I don't want to create a new object every time new information is given, I want to add it to an existing one.
That's fine, as long as you control the object there is no problem with this, some other may find easier to just create a new object.
Is using JavaBeans for data storage a bad practice and should be avoided, or is it perfectly safe?
No, is not a bad practice. Is not perfectly safe either. Depends on the situation.
The problem with mutable objects ( not with JavaBeans per se ) is using different threads to access them.
You have to synchronize the access to avoid one thread modify the object while other is accessing it.
Immutable objects doesn't have this problem, because, .. well they can't change, and thus, you don't have to synchronize anything.
To make sure an object is immutable you have to declare your attributes as final.
class MyBean {
private final int i;
}
If you want to assign a reasonable value to MyBean.i
you have to specify it in the constructor:
public MyBean( int i ) {
this.i = i;
}
Since the variable is final, you can't use a setter. You can just provide a getter.
This is perfectly thread-safe and the best is, you don't have to synchronize the access, because if two threads try to get the value of i
they both will always see the value that was assigned on instantiation, you don't have to synchronize anything.
Is not bad practice or good practice. Must of us have to work with a single thread, even in multithread environments like servlets.
If in the future you have to deal with multi thread applications, you may consider using an immutable JavaBean ;)
BTW, the alternative to create immutable beans, and still provide a bunch of setters is using Builders
like:
Employee e = new EmployeeBuilder()
.setName("Oscar")
.setLastName("Reyes")
.setAge(0x1F)
.setEmployeeId("123forme")
.build();
Which looks pretty similar to the regular setXyz used in regular beans with the benefit of using immutable data.
If you need to change one value, you can use a class method:
Employee e = Employee.withName( e, "Mr. Oscar");
Which takes the existing object, and copy all the values, and set's a new one....
public static EmployeeWithName( Employee e , String newName ){
return new Employee( newName, e.lastName, e.age, e.employeeId );
}
But again, in a single thread model is perfectly safe to use getters/setters.
PS I strongly encourage you to buy this book: Effective Java. You'll never regret about it, and you'll have information to judge better articles like one cited.
My objection to using JavaBeans as data storage classes is that they allow for objects with inconsistent state. In the typical use case for such beans, you have the following steps:
Now your class is ready to use. So what's the problem here? Between instantiating the class and setting the final property you have an object which is in an internally-inconsistent or unusable state, but there's nothing preventing you from accidentally using it. I prefer a system where the class is automatically in a consistent, usable state upon instantiation. For this reason I prefer to either pass in all the initial state in the constructor or, if said initial state is too complicated, pass in the initial state in the form of a hash map or set or the like. The use case scenario is now:
At no point in this work flow is it possible for me to accidentally start using an object with inconsistent state. If I use a parameters object, it is not directly used for anything at all and its contents will be vetted upon instantiation of my main class. The main class itself, upon returning from instantiation, will give me an object instance that is of immediate use.
This kind of setup is best for simpler objects, of course. For more complicated objects with things like optional properties, etc. you'll want to go a step farther and use something like the Builder pattern that others have pointed you toward. Builders are nice when you have more complicated scenarios, IMO, but for simpler, more straightforward parametrization just using constructor arguments or a parameters object of some sort is more than enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With