Let's say you've got a domain model class for a Song. Song's have a tempo attribute (an int) that should always be a positive number. Should this requirement be part of the domain model or externally (such as in a SongManager / business logic layer class)?
Let's say you've chosen to implement it like so:
class Song {
private int tempo;
// ...
public void setTempo(int tempo) {
if (tempo > 0) {
this.tempo = tempo;
} else {
// what?
}
}
}
Would you replace // what?
above with:
- Nothing. Given a Song instance
s
,s.setTempo(-10)
would simply not modify the object's state. - Set tempo to some minimum value, eg
1
. - Mark
setTempo
with a checkedthrows InvalidTempoException
andthrow
it in theelse
clause. That way, a controller or other component is responsible for catching the invalid tempo value and making a decision about how to handle the exception. - Throw a runtime InvalidTempoException.
- Extract the tempo attribute into a Tempo class with the "must be greater than 0" encapsulated there.
- Something else.
I ask because I've been exploring the common "layered architecture" approach lately and in particular the domain layer.
If a
Song
is not valid with a zero or negative tempo value, this certainly should be an invariant modelled withinSong
.You should throw an exception if an invalid value is attempted - this makes certain you don't have a
Song
in invalid state.