简体   繁体   中英

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. So we are talking about a class with 10 fields with only the field status that actually would make sense to have setters.

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. Plus, yes only status will have setter method.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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