为什么我不应该使用不可变的POJO而不是JavaBeans? [英] Why shouldn't I use immutable POJOs instead of JavaBeans?

查看:100
本文介绍了为什么我不应该使用不可变的POJO而不是JavaBeans?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经实现了一些Java应用程序,到目前为止只有桌面应用程序。我更喜欢使用不可改变的对象周边的传递数据的应用程序,而不是使用对象与增变器(setter方法和吸气剂),也称为JavaBeans的。



<但是在Java世界中,使用JavaBeans似乎更常见,我无法理解为什么我应该使用它们。就个人而言,如果代码只处理不可变对象而不是一直改变状态,那么代码看起来会更好。



第15项:最小化可变性 Effective Java 2ed ,也建议使用不可变对象。



如果我有一个 Person 实现为 JavaBean ,它将如下所示:

  public class Person {
private String name;
private Place birthPlace;

public Person(){}

public setName(String name){
this.name = name;
}

public setBirthPlace(Place birthPlace){
this.birthPlace = birthPlace;
}

public String getName(){
return name;
}

public Place getBirthPlace(){
return birthPlace;
}
}

相同的人实现为不可变对象:

 公共类人员{
private final String name;
私人决赛Place birthPlace;

public Person(String name,Place birthPlace){
this.name = name;
this.birthPlace = birthPlace;
}

public String getName(){
return name;
}

public Place getBirthPlace(){
return birthPlace;
}
}

或更接近结构 in C:

  public class Person {
public final String name;
public final Place birthPlace;

public Person(String name,Place birthPlace){
this.name = name;
this.birthPlace = birthPlace;
}
}

我也可以让不可变对象中的getter隐藏实施细节。但由于我只将它用作 struct ,我更喜欢跳过getters,并保持简单。



简单地说,我不明白为什么它是更好地使用JavaBeans的,或者如果我可以,也应该跟上我不变的POJO去?



许多Java库似乎对JavaBeans的更好的支持,但对于不可改变的POJO也许更多的支持随着时间的推移变得更受欢迎?


解决方案

体型的JavaBeans当




  • 你必须与期望它们的环境互动

  • 你有很多不方便的财产在实例化时进行所有初始化

  • 你的状态很昂贵或者因某些原因无法复制但需要变异

  • 你认为在某些时候你可能必须改变访问属性的方式(例如,从存储位置移动到计算值,访问权限等)。

  • 你想要符合编码st andards即盲目坚持它是某种更使用的JavaBeans 面向对象



体型不可变的POJO当




  • 您有少量简单属性

  • 您不必与假设JavaBean约定的环境进行交互

  • 克隆对象时很容易(或者至少可能)复制状态

  • 你根本没有计划克隆对象

  • 你很确定你不必像上面那样修改属性的访问方式

  • 你不介意听抱怨(或嘲笑) )关于你的代码如何不充分面向对象


I have implemented a few Java applications now, only desktop applications so far. I prefer to use immutable objects for passing the data around in the application instead of using objects with mutators (setters and getters), also called JavaBeans.

But in the Java world, it seems to be much more common to use JavaBeans, and I can't understand why I should use them instead. Personally the code looks better if it only deals with immutable objects instead of mutate the state all the time.

Immutable objects are also recommended in Item 15: Minimize mutability, Effective Java 2ed.

If I have an object Person implemented as a JavaBean it would look like:

public class Person {
    private String name;
    private Place birthPlace;

    public Person() {}

    public setName(String name) {
        this.name = name;
    }

    public setBirthPlace(Place birthPlace) {
        this.birthPlace = birthPlace;
    }

    public String getName() {
        return name;
    }

    public Place getBirthPlace() {
        return birthPlace;
    }
}

And the same Person implemented as an immutable object:

public class Person {
    private final String name;
    private final Place birthPlace;

    public Person(String name, Place birthPlace) {
        this.name = name;
        this.birthPlace = birthPlace;
    }

    public String getName() {
        return name;
    }

    public Place getBirthPlace() {
        return birthPlace;
    }
}

Or closer to an struct in C:

public class Person {
    public final String name;
    public final Place birthPlace;

    public Person(String name, Place birthPlace) {
        this.name = name;
        this.birthPlace = birthPlace;
    }
}

I could also have getters in the immutable object to hide the implementation details. But since I only use it as a struct I prefer to skip the "getters", and keep it simple.

Simply, I don't understand why it's better to use JavaBeans, or if I can and should keep going with my immutable POJOs?

Many of the Java libraries seem to have better support for JavaBeans, but maybe more support for immutable POJOs gets more popular over time?

解决方案

Prefer JavaBeans When

  • you have to interact with environments that expect them
  • you have lots of properties for which it would be inconvenient to do all initialization on instantiation
  • you have state that is expensive or impossible to copy for some reason but requires mutation
  • you think at some point you may have to change how properties are accessed (e.g. moving from stored to calculated values, access authorization, etc.)
  • you want to conform to coding standards that mindlessly insist it is somehow more "object-oriented" to use JavaBeans

Prefer Immutable POJOs When

  • you have a small number of simple properties
  • you do not have to interact with environments assuming JavaBean conventions
  • it is easy (or at the very least possible) to copy state when cloning your object
  • you don't ever plan on cloning the object at all
  • you're pretty sure that you don't ever have to modify how properties are accessed as above
  • you don't mind listening to whining (or sneering) about how your code isn't sufficiently "object-oriented"

这篇关于为什么我不应该使用不可变的POJO而不是JavaBeans?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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