[英]Using an immutable or mutable object in this java context?
I have a class Account
with many fields and only the field status
(active or inactive) may change in the object. 我有一个包含许多字段的类
Account
,并且仅对象中的字段status
(活动或不活动)可以更改。 So we are talking about a class with 10 fields with only the field status
that actually would make sense to have setters. 因此,我们谈论的是一个具有10个字段的类,而只有字段
status
对于设置员来说才有意义。
What would you do in this scenario, would you make the whole class immutable besides that particular field which changes status? 在这种情况下,您会做什么?除了改变状态的特定字段之外,您是否会使整个类变得不可变?
Account
need not be immutable, but its composed classes as fields if any would be immutable. Account
不必是不可变的,但它的组合类作为字段(如果有的话)将是不可变的。 Plus, yes only status
will have setter method. 另外,是的,只有
status
将具有setter方法。
Something like below - 如下所示-
class Account {
private int a;
private String b;
private SomeClass s; //Make this immutable
private String status;
String setStatus(String status) {
this.status = status;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.