What is a good use case for Java Beans?

I just saw the Java Beans specification. I felt that using just getters, setters, and an empty constructor would make the code more stressful, and the developer would have to manage the track to set all the required instances correctly.

Can anyone point me to a good example of using such a design? I cannot figure it out. There is one use I can think of - "When you need to instantiate a class so that its data members are defined later in code."

EDIT: Guys, I am not initiating a discussion. I don't want to discuss the advantages / disadvantages. I am not looking for current libraries like spring etc. which makes it mandatory to use Beans. I am looking for an example to understand the technical capabilities that Java Beans will bring. "Just One Example Where Java Beans Design Will Help

+3


source to share


6 answers


Several libraries and specifications (eg JPA, JavaEL) use the Java Beans specification and rely on this behavior.

Using getters and setters has the advantage of allowing you to add code inside these methods, for example. to create "virtual" properties that are calculated at run time rather than being stored in the field.

In addition, using getters and setters allows other frameworks to wrap these methods and provide additional functionality like change logging, etc. In many cases, this is done by internally subclassing and overriding getters and setters. The user would not notice this as this "weaving" or "proxying" is often done at runtime. For example, Hibernate uses this to provide lazy loading functionality when you call getter to access the corresponding collection or entity.

Update

As requested, an example for "virtual" properties:



Suppose you have a Person

bean that has fields firstName

and lastName

. You can add a read-only virtual property by name

providing the following getter:

public String getName() {
  return getFirstName() + " " + getLastName();
}

      

Update 2:

Another note on why getters and setters are needed: it mostly has to do with how Java works. Languages โ€‹โ€‹that directly support properties, such as C #, will allow you to write type code person.firstName = "Joe";

and still use the setter if there is one, or throw an error if the property is read-only. Therefore, if you add an installer for firstName

, these languages โ€‹โ€‹will internally translate firstName = "Joe"

to setFirstName("Joe")

, if the developer does not need to change anything - quite an elegant solution. :)

Since Java does not support this, we should always provide accessor methods (setters / getters), even if they don't do anything special - in case they might be changed in the future.

+4


source


getters and setters provide - the basic object oriented concept

Encapsulation

Imagine that you have written the code for a class and a dozen more programmers from your company have all written programs that used your class. So, anyone can change your instance variable, and this can harm your code.

So you must

Keep instance variables protected

(c access modifier, often private

).



Make public accessor methods

and force your code to use these methods . rather than directly accessing the instance variable

Their code revealed errors in your code.

see the following example

public class MyClass{
public int size;
public int weight;
...
}
public class OthersClass {
public static void main (String [] args) {
MyClass myClass= new MyClass();
myClass.size = -5; // Legal but bad logic
}
}

      

+1


source


An empty constructor is required to create a new instance by reflecting persistence in your structure. Unless you provide any additional constructors with arguments for the class, you don't need to specify an empty constructor because you get one by default.

In my experience, this prevents someone adding a parameterized constructor and thereby effectively removing the default constructor. By implementing the default constructor explicitly, which is unlikely to happen.

Also, I am assuming that you envision a class with multiple attachรฉs that would be difficult to fill with just getters and setters, but also note the following points:

  • Getters automates the submission portion.
  • Retailers allow you to add validation or error control as needed.
+1


source


Apart from the actual advantages and disadvantages of the JavaBeans specification, I think it is important to remember that this is a specification introduced in 1996 to enable visual editing of Java code.

I don't know if this programming approach was successful and accepted (I've never used it personally). I know the spec has continued to live on and be applied very widely (IMHO sometimes also in contexts in which it was not the most appropriate).

I think the most useful place to use JavaBeans is in the framework . Spring is a good example. The JavaBean class is very suitable when the instance of the class is not responsible for the programmer, but is delegated to the application container (for example, through reflection).

+1


source


JavaBeans, as you described above, can be used as DAOs (Data Access Objects) to retrieve and save data to a SQL database. They are useful where all you really need is data.

0


source


Many will tell you that Java Beans are much better than using simple Java objects that act as "data transfer objects".

But other people, like this guy here in his famous book https://cleansourcecode.files.wordpress.com/2013/10/clean-code.pdf suggests that more often than not, "beans" and receivers and setters have little more meaning (this chapter is dedicated to this chapter).

So you have to decide for yourself (and probably with your teammates) if your problem is better solved with "Java Beans" or "simple data transfer objects".

Of course, beans force you to use setters, which allow you to add validation later. On the other hand: such a change changes the semantics of your methods; and it might be completely wrong that all of a sudden the setter does the check and starts throwing exceptions. Because if you change the contract of your methods; you may not want to do this implicitly under the covers.

0


source







All Articles