POJO有什么优势? [英] What's the advantage of POJO?

查看:95
本文介绍了POJO有什么优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个小数据结构 Key

In my project I have a small data structure Key.

public class Key implements Serializable {

  private static final long serialVersionUID = 1L;

  public String db;
  public String ref;
  public Object id;

  protected Key() {
  }

  public Key(String db, String ref, Object id) {
    this.db = db;
    this.ref = ref;
    this.id = id;
  }
}

是的,这个类很简单,每个字段都可公开访问。

Yes this class is simple and every field is publicly accessible.

但有人建议我使用 POJO 风格的课程,但当我问他们为什么无法告诉我时。

But someone has suggested I use POJO style classes instead but when I asked why they were unable to tell me.

在我看来,拨打 getters setters 比直接访问某个字段要慢。

In my opinion , calling getters and setters is slower than direct access to a field.

为什么我必须使用 POJO 编程风格?

So why I must use POJO programming style?

推荐答案

取自维基百科:


POJO是Plain Old Java Object的首字母缩写。该名称用于
强调给定对象是普通Java对象,而不是
特殊对象。

POJO is an acronym for Plain Old Java Object. The name is used to emphasize that a given object is an ordinary Java Object, not a special object.

POJO通常很简单,因此不依赖于其他库,接口或注释。这增加了在多个项目类型(Web,桌面,控制台等)中重用它的可能性。

A POJO is usually simple so won't depend on other libraries, interfaces or annotations. This increases the chance that this can be reused in multiple project types (web, desktop, console etc).

正如有人已经在评论中指出的那样,你的对象是技术上已经是一个POJO但是你已经特别询问了更类似于JavaBeans的getter和setter。

As someone has already pointed out in the comments, your object is technically a POJO already however you have specifically asked about getters and setters which are more akin to JavaBeans.

我有很多理由可以考虑使用getter和setter :

There are a number of reasons I can think of for using getters and setters:


  1. 您可能只想获取某些值(IE只读值) )。对于字段,客户端可以直接获取和设置值。如果字段被标记为final,则字段可以是只读的,尽管这并不总是保证它们是不可变的(参见第9点)。

  2. Getter& setter方法允许您在不破坏类的公共接口的情况下更改基础数据类型,这使得它(和您的应用程序)更加强大并且能够适应变化。

  3. 您可能想要调用一些其他代码,例如在获取或更改值时发出通知。对于您当前的类,这是不可能的。

  4. 您正在公开您的类的实现,在某些情况下可能存在安全风险。

  5. Java bean是围绕POJO设计的,这意味着如果你的课程没有被实现,那么某些工具和图书馆不能使用这些工具和图书馆来使你的班级遵守这些完善的原则。

  6. 您可以公开由字段IE 支持的值计算值,例如 getFullName(),它是 getFirstName() getLastName的串联( )由字段支持。

  7. 您可以向setter方法添加验证,以确保传递的值正确。这可以确保您的类始终处于有效状态。

  8. 您可以在getter和setter中设置断点,以便在获取或更改值时调试代码。

  9. 如果字段是对象(IE不是基本类型),那么类的内部状态可以被其他对象修改,这可能导致错误或安全风险。您可以通过返回对象的副本来保护POJO的getter中的这种情况,以便客户端可以处理数据而不会影响对象的状态。请注意,拥有最终字段始终保护您免受此类攻击,因为客户端仍然可以对正在引用的对象进行更改(假设该对象本身是可变的)您一旦设置完毕,就无法将字段指向不同的引用。

  1. You might only want to get some of the values (I.E. read only values). With fields, clients can both get and set the values directly. Fields can be made read-only if they are marked as final although this doesn't always guarantee that they are immutable (see point 9).
  2. Getter & setter methods allow you to change the underlying data type without breaking the public interface of your class which makes it (and your application) more robust and resilient to changes.
  3. You might want to call some other code such as raising a notification when the value is obtained or changed. This is not possible with your current class.
  4. You are exposing the implementation of your class which could be a security risk in some cases.
  5. Java beans are designed around POJO's which means that if your class is not implemented as one it can't be used by certain tools and libraries that expect your class to adhere to these well established principles.
  6. You can expose values that are not backed by a field I.E. calculated values such as getFullName() which is a concatenation of getFirstName() and getLastName() which are backed by fields.
  7. You can add validation to your setter methods to ensure that the values being passed are correct. This ensures that your class is always in a valid state.
  8. You can set a breakpoint in your getters and setters so that you can debug your code when the values are obtained or changed.
  9. If the field is an object (I.E. not a primitive type) then the internal state of your class can be modified by other objects which can lead to bugs or security risks. You can protect against this scenario in your POJO's getter by returning a copy of the object so that clients can work with the data without affecting the state of your object. Note that having a final field does not always protect you against this sort of attack as clients can still make changes to the object being referenced (providing that object is itself mutable) you just cannot point the field at a different reference once it has been set.

是,访问或设置通过方法调用的值可能比直接字段访问慢,但差别几乎不明显,它肯定不会成为您程序中的瓶颈。

Yes, accessing or setting the values via method calls may be slower than direct field access but the difference is barely noticeable and it certainly won't be the bottleneck in your program.

虽然优点是明确这并不意味着吸气剂和制定者是银弹。在设计真实世界,强大的可扩展类时,需要考虑许多陷阱。

Whilst the advantages are clear this does not mean that getters and setters are a silver bullet. There are a number of 'gotchas' to consider when designing real world, robust scalable classes.

这个回答一个非常相似的问题,在设计一个有getter和setter的类时会详细考虑一些注意事项。虽然根据您正在设计的班级类型,建议可能更相关。一个类,它构成一个大型系统中API的一部分,而不是一个简单的数据传输对象。

This answer to a very similar question looks at some considerations in detail when designing a class that has getters and setters. Although the suggestions may be more relevant depending on the type of class you are designing E.G. a class that forms part of an API in a large system as opposed to a simple data transfer object.

另请注意,某些情况下某个类具有直接字段可能是有利的,例如当速度必不可少或内存有限时尽管这应该仅在分析代码并发现它实际上是瓶颈后才考虑

Also note that there may be certain scenarios where a class with direct field may be advantageous such as when speed is essential or memory is limited although this should only be considered after profiling your code and finding that it is actually a bottleneck.

另外要注意的是,您不仅要在getter和setter中包含所有字段,因为这确实缺少了封装点。

Also be careful that you are not just wrapping all of your fields in getters and setters as this is really missing the point of encapsulation.

这个答案很好地总结了使用getter和setter选择POJO而不是JavaBean样式对象的原因。

This answer provides a good summary of the reasons for choosing a POJO over a JavaBean style object with getters and setters.

这篇关于POJO有什么优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆