Created
September 20, 2018 14:02
-
-
Save lipeRomani/7b75c30f9bee8e97b2b5d4683b21617e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note na definição do serviço acima que ele possui muitas responsabilidades e podemos ainda adicionar mais, caímos no antipattern "God class", ela não é coesa, pois não possui uma responsabilidade bem definida, perde a escalabilidade pois para aterarmos qualquer uma das responsabilidades precisamos mexer na mesma base de código, por exemplo podemos quebrar o "create" pelo simples falo de mexer no "changeStatus", de forma simples podemos arrumar isso. | |
interface UserService { | |
User create(User user) throws DuplicatedEntityException; | |
User update(User user) throws EntityNotFoundException; | |
Optional<User> findUser(FilterUser filter) throws DatabaseExeption; | |
void delete(User user) throws EntityNotFoundException; | |
void changeStatus(User user, Status newStatus) throws EntityNotFoundException; | |
void sendWelcomeEmail(User user) throws EntityNotFoundException, InvalidEmailException; | |
} | |
// Agora quebramos os serviços em especificações muito mais coesas, e também desta forma o código ficou muito mais escalável, por exemplo, caso quisermos alterar a implementação do "Welcome E-mail" apenas alteramos a implementação da interface, não precisamos mexer na mesma base de código do "Create User" por exemplo, isso é um passo que nos permite atingirmos o "O" (open/closed principle) do SOLID. | |
// Definimos de forma mais clara as responsabilidades e ficou muito mais fácil de criar testes. | |
interface CreateUserService { | |
User create(User user) throws DuplicatedEntityException; | |
} | |
interface UpdateUserService { | |
User update(User user) throws EntityNotFoundException; | |
} | |
interface SearchFilteredUserService { | |
Optional<User> findUser(FilterUser filter) throws DatabaseExeption; | |
} | |
interface DeleteUserService { | |
void delete(User user) throws EntityNotFoundException; | |
} | |
interface ChangeUserStatusService { | |
void changeStatus(User user, Status newStatus) throws EntityNotFoundException; | |
} | |
interface SendWelcomeEmailService { | |
void sendWelcomeEmail(User user) throws EntityNotFoundException, InvalidEmailException; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment