I have more or less implemented the command pattern in my Spring Boot application. I still have some doubts and would like to get second opinion on them.
I have a CommandFactory
which - when given a command code - returns an instance of command object that implements Command
functional interface and has a single method called execute
.
Here's an example of use:
- PersonService:
public Person addChildToPerson(Long personId, Long childId) {
// acquire a person object from the database given an id
Person person = (Person) commandFactory.create(GET_PERSON_BY_ID, personId).execute();
// do the same with child
Child child = (Child) commandFactory.create(GET_CHILD_BY_ID, childId).execute();
return (Person) commandFactory.create(ADD_CHILD_TO_PERSON, person, child).execute();
}
Now here's the code of a AddChildToPersonCommand
class that is responsible for attaching the child to the person and persisting it in the database.
- AddChildToPersonCommand:
@RequiredArgsConstructor
public class AddChildToPersonCommand implements Command {
// declare needed components for performing the command
private final ChildRepository childRepository;
private final Person person;
private final Child child;
@Override
public Person execute() {
// some logic
}
}
Now, my doubts are mainly concerned with the "declared components" for the execution of a command. I'm unsure whether it's a better practice to operate on the actual objects that have been already retrieved using other commands (in the example: GetPersonByIdCommand
& GetChildByIdCommand
) or to just give the AddChildToPersonCommand
the IDs and let it take care of the rest.
On the one hand, it seems a better approach to operate on the objects as it decreases the responsibilites of AddChildToPersonCommand
.
On the other hand, though, the PersonService's methods may end up really big when the logic to-be-performed is not as trivial as in this example.
What do you think?
Aucun commentaire:
Enregistrer un commentaire