Is the POJO class an object?


I don't quite understand why a POJO object is so convenient and how to understand that it is a POJO.

By definition, this is an object that does not extend anything, does not implement anything, and does not have a constructor, all variables are private + getters and setters... Just such a simple object.

So, if I understand correctly, if we let's throw out a constructor from this class, then it's like POJO already?

public final class CardFriend {

private String friendName;
private String friendPhoneNumber;
private Bitmap friendPhotoBitmap;
private String friendEmail;
private String Uid;
private int chanel;

public CardFriend(String friendName, String friendPhoneNumber, Bitmap photoBitmap, String friendEmail,
                  int chanel) {
    this.friendPhotoBitmap = photoBitmap;
    this.Uid = "test";

    if (friendName == null){
        this.friendName = States.NO_NAME;
    }else {
        this.friendName = friendName;
    }

    if (friendPhoneNumber == null){
        this.friendPhoneNumber = States.WITHOUT_PHONE_NUMBER;
    }else {
        this.friendPhoneNumber = friendPhoneNumber;
    }

    if (friendEmail == null) {
        this.friendEmail = States.WITHOUT_EMAIL;
    } else {
        this.friendEmail = friendEmail;
    }

    this.chanel = chanel;
}

public String getFriendName() {
    return friendName;
}

public String getFriendPhoneNumber() {
    return friendPhoneNumber;
}

public Bitmap getFriendPhotoBitmap() {
    return friendPhotoBitmap;
}

public String getFriendEmail() {
    return friendEmail;
}

public int getChanel() {
    return chanel;
}

public String getUid() {
    return Uid;
}
}

And I have not found whether it is possible to use logical methods in such cases. classes? Let's assume the method of some calculation.

Well, if someone gives examples where such objects are best used (since it is not a dubious advantage to throw out the constructor and put setters), it will be quite clear))

Author: MSDN.WhiteKnight, 2016-10-23

1 answers

In a report by Martin Fowler, Rebecca Parsons, and Josh McKenzie from 2000, when the term was first used, POJO is just a Java class without "bells and whistles", not tied to a specific framework. In the context of that report, POJOs are simple objects that are not Entity Beans from J2EE. A clear definition was not given at the time, so the points of view on what it is may differ slightly.

As a rule, such classes do not inherit from other classes (probably, you can do an exception for POJO classes from the same package), do not implement interfaces (sometimes an exception is made for marker interfaces from the standard library), do not use annotations in definitions, and do not depend on third-party libraries. I.e., POJO can have both methods with business logic and arbitrary constructors.

If you make an exception for Serializable, then JavaBeans can be assigned to POJO. If you allow annotations that do not change the semantics of the class (i.e. without which the assignment the object and the logic of its operation will not change), then POJO can be attributed to JPA entities and DTO objects that are serialized in XML or JSON, the rules for which are set in the annotations.

An example of when it is convenient to use POJO is an ordinary domain model in DDD, which you store in the database or pass through a REST service to the client. You can cram a bunch of annotations into it, or you can put all the mapping rules on the database or DOM into separate components (ORM configurations and serializer), reducing the connectivity of your architecture.

 9
Author: Ivan Gammel, 2016-10-23 18:38:37