Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Beans: What am I missing?

Tags:

java

javabeans

I'm wondering if I'm missing something about Java Beans. I like my objects to do as much initialization in the constructor as possible and have a minimum number of mutators. Beans seem to go directly against this and generally feel clunky. What capabilities am I missing out on by not building my objects as Beans?

like image 510
Dave Ray Avatar asked Nov 24 '08 19:11

Dave Ray


People also ask

Which of the following is not about JavaBeans?

Which of the following is not a feature of Beans? Explanation: Serialization is not the feature of Java Beans. Introspection, Customization, Events, Properties and Persistence are the features.

What are the elements of JavaBeans?

Components of JavaBeans It consists of events, methods, persistence, properties. Non-GUI based and GUI based components are the two types of components.

What is the point of a JavaBean?

The main reason for JavaBeans is for reusability. JavaBeans can be used in JSP's, Servlets, and other java technologies. It's a simple serializable objects that is used to encapsulate many objects into one. I.e, we can send a bean fully defined as to sending each attribute individually across the wire.


2 Answers

It sounds like you are on the right track. It's not you who's missing the point of Java Beans, it is other programmers that are misusing them.

The Java Beans specification was designed to be used with visual tools. The idea was that an application designer would be able to configure an instance of an object interactively, then serialize (or generate code for) the configured bean, so that it could be reconstructed at runtime; the intent was that it would not be mutated at runtime.

Unfortunately, a lot of developers don't understand that accessors violate encapsulation. They use structs instead of objects. They don't see anything wrong with other classes, even other packages, having dependencies on a class's data members.

Of course, you will in general have the need to configure instances of your objects. It's just that this should be done through some sort of configuration feature. This might be a dependency injection container, a "BeanBox" style visual tool, or simply reading JSON, XML, or properties files that you wrote by hand. The key is that at runtime these objects are effectively immutable; clients just invoke their operations, they don't access their properties.

like image 155
erickson Avatar answered Sep 21 '22 23:09

erickson


I like my objects to do as much initialization in the constructor as possible and have a minimum number of mutators.

Favouring immutable objects is a wise choice. However the benefit of beans is that frameworks/tools/libraries can determine at runtime the properties of a class, without requiring you to implement a particular interface.

For example, assume you have a collection of Person beans, and each bean has properties such as name, age, height, etc.

You can sort this collection of beans by name (for example) using the following code:

Collection<Person> myCollection = // initialise and populate the collection
Comparator nameCompare = new BeanComparator("name");
Collections.sort(myCollection, nameCompare);

The BeanComparator class knows how to extract the "name" property from each object, because the Java Beans convention is followed, i.e. you're spared the "overhead" of implementing an interface such as:

interface Nameable {
    public String getName();
    public void setName(String name);
}

Another example from Spring MVC is a bean that stores request URL parameters. This can be defined in a web controller (known as an 'Action' in Struts) like this:

public ModelAndView searchUsers(UserSearchCriteria criteria) {
    // implementation omitted
}

Because UserSearchCriteria is expected to be a JavaBean, if the request URL contains a parameter such as maxItems=6, the Spring framework 'knows' that it should call a method with the signature

void setMaxItems(int maxItems);

In essence, JavaBeans is just a simple convention which allows the properties of a class to be discovered dynamically at runtime (usually by a tool or framework), when it's inconvenient/impossible to know a priori the properties that may be provided.

like image 44
Dónal Avatar answered Sep 23 '22 23:09

Dónal