To use a dumb example, assume that particular users (labourers) can move boxes around. How would you go about representing that in your system in a semantic way that scales. I'm currently inclined to do the following.
class User {
// ....
lift(box: Box) {
if(this.role === UserRoles.Labourer) {
box.setPosition(box.xPosition, box.yPosition + 10);
} else {
// whatever
}
}
}
I was thinking of going in this direction because it captures the semantic that a user can lift
a box. But since the change is actually happening to the box, we still need a method to change the box's properties (i.e create a setPosition
method to set location of the box). Do you think this decision is appropriate? Would you model interactions like this by providing a method on the subject of the verb along with a corresponding method on the object? I'm asking this as I am trying to capture semantics of my business in code in an expressive way and wonder if this was ever something that was thought of.
In addition, is there a better way of capturing the constraint of only a user with the the Labourer
can do this action. Is it best to put this check in the location I have put it? Is it best done in the Box class? Do you have thoughts on this?