Java Bean encapsulates many objects into one,How?

夙愿已清 提交于 2020-06-13 07:02:50

问题


I'm newly started reading about Java Beans and I had a question which was exactly same as this Topic's question. So I repeat The question:

in definition it is said "java bean encapsulates many objects into one object(Bean)."

1.What does "many objects" here mean?

and

2.How they are encapsulated into one object by java beans?


Edit:

From Java Beans Wikipedia:

in computing based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object (the bean).


Edit2: all of classes have ability of having multiple attributes and fields. If encapsulating of many objects means having multiple attributes and fields, I don't understand why they mentioned to this ability as a advantage of java bean class.


回答1:


First to make it clear, every Class in Java extends the type Object. Something like String is also an Object.

The "many objects" is referring to how we can use different objects as fields within the bean. This creates a has-a relationship with the bean to your Objects.

For example, say we have this Bean:

public class YourBean implements java.io.Serializable {

    private String s;
    private ArrayList<String> list;

    //Omitted rest of bean boilerplate
}

This example will contain two different Objects inside of it, the String s and the ArrayList<String> named list. You can add as many different Objects and primitives to your bean as you want.

To create the bean with a no-args constructor, you would then use:

YourBean bean = new YourBean();

And you can set and get the values of the Objects encapsulated within with:

ArrayList<String> yourList = new ArrayList<>();
bean.setList(yourList);
System.out.println(bean.getList());

You will be able to refer to all the Objects inside the bean this way by referencing the bean Object I named bean.

Additionally, you can create multiple of the same type of bean as well, so every time you make a new YourBean(), you will also be able to use all the Objects contained within.

This functionality is not unique to a Bean, you can do this in any Class, rather a Bean is a term used to describe a specific way you write some classes.

I recommend looking into Java Composition to learn when you should use a has-a relationship, rather than inheritance which is an is-a relationship.




回答2:


We usually talk about Spring beans, but I think that's not what you're talking about. It seems to me that those JavaBeans are nothing but classes with multiple attributes and only getters/setters but whose constructor has zero arguments (and therefore it is mutable). As simple as that. The fact of encapsulating many objects is due to it having multiple attributes.

However, I have never referred to them as JavaBeans and I think the most similar concept I have ever worked with are the POJOs. I'm not sure if there is any difference, but the purpose looks the same.

If you ever talk about a bean in Java, I think anyone will think of a Spring bean. I suggest you not to use it in another context.

This is just my guessing. If I've said anything wrong please tell me.




回答3:


JAVA Beans

The concept of JavaBeans was originally devised for Swing to facilitate the development of standalone GUI components, but the pattern has been repurposed for the land of Spring beans and back-end persistence with Hibernate

On the other hand, POJOs are simple java classes.


Another View:

Any POJO on having interaction with some third party become JAVA BEAN :)

  • Java Classes interacting with any ORM(say Hibernate)
  • Java Classes being used as Session Objects in EJB

As they say, "With Great Powers Comes Great Responsibilities" [excerpt from spiderman]

So our normal Pojos become JAVA BEANS :)

An article worth going through: https://mossgreen.github.io/Java-Bean-VS-Spring-Bean/



来源:https://stackoverflow.com/questions/60436733/java-bean-encapsulates-many-objects-into-one-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!