mardi 9 août 2022

Should I use Builder Pattern Or a Regular Constructor For Service Classes?

When doing dependency injection without a framework (i.e. for a library), is it better practice to create a required args constructor for service classes or a builder for service classes? Is one more testable than the other?

@RequiredArgsConstructor
class MyServiceClass {
      private final Dependency1;
      private final Dependency2;
      private final Dependency3;
}

Vs.

@Builder
class MyServiceClass {

    @Nonnull
    private final Dependency1;

    @Nonnull
    private final Dependency2;

    @Nonnull
    private final Dependency3;

}

Aucun commentaire:

Enregistrer un commentaire