Difference between DTO, VO, POJO, JavaBeans?

后端 未结 7 1748
误落风尘
误落风尘 2020-11-22 07:46

Have seen some similar questions:

  • What is the difference between a JavaBean and a POJO?
  • What is the Difference Between POJO (Plain Old Java Object) an
7条回答
  •  死守一世寂寞
    2020-11-22 08:16

    DTO vs VO

    DTO - Data transfer objects are just data containers which are used to transport data between layers and tiers.

    • It mainly contains attributes. You can even use public attributes without getters and setters.
    • Data transfer objects do not contain any business logic.

    Analogy:
    Simple Registration form with attributes username, password and email id.

    • When this form is submitted in RegistrationServlet file you will get all the attributes from view layer to business layer where you pass the attributes to java beans and then to the DAO or the persistence layer.
    • DTO's helps in transporting the attributes from view layer to business layer and finally to the persistence layer.

    DTO was mainly used to get data transported across the network efficiently, it may be even from JVM to another JVM.

    DTOs are often java.io.Serializable - in order to transfer data across JVM.

    VO - A Value Object [1][2] represents itself a fixed set of data and is similar to a Java enum. A Value Object's identity is based on their state rather than on their object identity and is immutable. A real world example would be Color.RED, Color.BLUE, SEX.FEMALE etc.

    POJO vs JavaBeans

    [1] The Java-Beanness of a POJO is that its private attributes are all accessed via public getters and setters that conform to the JavaBeans conventions. e.g.

        private String foo;
        public String getFoo(){...}
        public void setFoo(String foo){...}; 
    

    [2] JavaBeans must implement Serializable and have a no-argument constructor, whereas in POJO does not have these restrictions.

提交回复
热议问题