Why setters are bad in interfaces if we are speaking about domain objects?
Clarification:
I have a domain object which stores in db. It has several fields which is very costly to set. I.e.
class JurasicPark {
private long area;
...other fields ...
getters and setters
....
private Collection<Dinosaur> dinosaurs;
private Collection<ExoticTree> flora;
public Collection<Dinosaur> getDinosaurus(){
...
}
public Collection<ExoticTree> getFlora(){
...
}
}
Fields dinosaurs
and flora
are very costly to init and set. But in many cases I don't need this fields to be set every time.
The problem is that if I return to user instance of JurasicPark class with dinosaurs
or flora
don't initialized it fill lead either to NPE or some expection I throw myself. I don't want to make api user to think about this and to remember which fields may be not set.
So to solve this I thought about creating two interfaces IJurasicPark
and IFullJurasicPark
the first will declare all access methods to simple fields, former will declare access methods to the flora
and dinosaurs
fields.
interface IFullJurasicPark extends IJurasicPark
class IJurasicPark implements IFullJurasicPark
In this approach IJurasicPark interface will contain getters and setters, so I actually asks is this design is bad one?
I also don't want to implement it in hibernate style with LazyInit exceptions.