次のクラスがあることを提案しましょう
Public MyClass{
Date date;
//Mutable getters and setters
public Date getdate(){
retrurn date;
}
public Date setdate(Date date)
{date = date;}
//Now immutable setter and Getters
public Date getImmutableDate{
return new Date(date.getDate());
}
public void setImmutableDate(Date mydate)
{
date = new Date(mydate.getDate());
}
}
Spring または Hibernate Framework に変更可能なゲッターとセッターを使用し、カスタム コーディングに不変のセッターとゲッターを使用することは良い習慣ですか。私が恐れているのは次のことです。
MyClass myclass = new MyClass();
//assuming that item date was initalized some how.
Date currentDate = myClass.getdate();
currentDate.setMonth( currentDate.getMonth() + 1 );
myClass オブジェクト内の日付変数の値を変更しただけで、システムのバグの主な原因になる可能性があります。
私は正しいですか?Spring/Hibernate にも不変のゲッターとセッターを使用できますか?