简体   繁体   English

创建具有类字段或简单字段的pojo的良好实践是什么

[英]What is good practice to create pojo as having Class fields or simple

What is good practice to create pojo as having Class fields or simple fields. 创建具有类字段或简单字段的pojo的最佳实践是什么。 I am creating pojo like this. 我正在这样创建pojo。

public class StatusDTO {

private String id;
private int totalNodes;
private int totalServlets;
private boolean status;
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}


public int getTotalNodes() {
    return totalNodes;
}

public void setTotalNodes(int totalNodes) {
    this.totalNodes = totalNodes;
}

public int getTotalServlets() {
    return totalServlets;
}

public void setTotalServlets(int totalServlets) {
    this.totalServlets = totalServlets;
}

public boolean isStatus() {
    return status;
}

public void setStatus(boolean status) {
    this.status = status;
}

} someone recommanded me to do like this as below }有人要求我这样做如下

public class StatusDTO {

private String id;
private boolean status;
private Total total;

public Total getTotal() {
    return total;
}

public void setTotal(Total total) {
    this.total = total;
}
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public boolean isStatus() {
    return status;
}

public void setStatus(boolean status) {
    this.status = status;
}
public static class Total {
    private int nodes;
    private int servlets;
    public int getNodes() {
        return nodes;
    }
    public void setNodes(int nodes) {
        this.nodes = nodes;
    }
    public int getServlets() {
        return servlets;
    }
    public void setServlets(int servlets) {
        this.servlets = servlets;
    }

}

} }

what difference does it make? 有什么区别? what is good practice among those two? 这两者之间的良好做法是什么? I am using this class to set db info and send info to web socket client(stomp) 我正在使用此类来设置数据库信息并将信息发送到Web套接字客户端(stomp)

Well, one important thing in a good Java application is separation of concerns, for example in an airport application a service that give the last flight of a customer should not require as parameter an object with the first name, the last name, the social security number, the marital status, the gender or whatever other information about the customer that are completely useless (or should be) in retrieving the customer last flight, such that you need to have an object Customer (with all customer information) and another object CustomerId (with only the necessary bits to get the flights). 好的,好的Java应用程序中的一个重要事情是关注点的分离,例如,在机场应用程序中,提供客户最后一次飞行的服务不应要求具有名字,姓氏,社会保障的对象作为参数。编号,婚姻状况,性别或与客户有关的其他任何信息,这些信息在检索客户的最后一次飞行中是完全无用的(或应该是),因此您需要一个对象Customer(包含所有客户信息)和另一个对象CustomerId (只有获得飞行所需的位)。

Another example is for a online shop application, a service that calculate the total price of the basket should not require all the information about all articles (photos, description, specifications, ...) in the basket but only the prices and the discounts which should be enclosed in another object. 另一个例子是对于在线商店应用程序,计算购物篮总价的服务不应该要求购物篮中所有商品(照片,描述,规格等)的所有信息,而只需要价格和折扣即可。应该放在另一个对象中。

Here you have to decide if the concerns of your Total (you need a better name) object could be taken separately of the concerns of your StatusDTO object, such that a method could require only the Total object without the associated StatusDTO object. 在这里,您必须决定是否可以将Total(需要一个更好的名称)对象的关注点与StatusDTO对象的关注点分开考虑,这样一个方法可以只需要Total对象而没有关联的StatusDTO对象。 If you can take them separately then you should have separate objects, if you can't then it's unnecessary. 如果可以将它们分开存放,则应该有单独的对象,如果不能分开存放,则没有必要。

The answer, as always in such questions, is: It depends. 像在此类问题中一样,答案是:它取决于。

Simple classes like the first one have the advantage that they are simpler and smaller. 像第一个这样的简单类的优点是它们更简单,更小。 The advantage on the second attempt is that if your class, maybe now, maybe later, gets extended, it might be easier if you create a separate Total class. 第二次尝试的好处是,如果您的班级(也许现在,以后)扩展了,那么创建单独的Total类可能会更容易。

Good Objectoriented Programming, and Java is strongly OO, almost always requires you to put everything into it's own class. 良好的面向对象程序设计,并且Java是高度面向对象的,几乎总是需要您将所有内容放入其自己的类中。

As a rule of thumb, I create a separate class if: 根据经验,如果满足以下条件,我将创建一个单独的类:

  • there is some functionality you to your fields. 您的字段有一些功能。
  • you have more then two, mabye three fields related to each other (eg connectionHost, connectionPort) 您有两个以上,彼此关联的三个字段(例如,connectionHost,connectionPort)
  • it's just a model class (eg Customer, Article) 这只是一个模型类(例如,客户,文章)
  • I can use the field in multiple other classes 我可以在其他多个班级中使用该字段

Of course there are more but those are some of the most important ones (comment if you think there is another important one I forgot to mention). 当然还有更多,但是其中一些是最重要的(如果您认为还有我忘记提及的另一个重要问题,请评论)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM