Answer the question
In order to leave comments, you need to log in
How to properly develop the architecture of an object and its services?
Hello. A question for experienced OOP design connoisseurs. Let's imagine a simple system where there is a client and a service serving it.
public interface ClientService {
void disableClient(String id);
void sendEmailToClientWithId(String id);
}
public class Client {
private String id;
private String username;
private String email;
private Boolean isActive = true; // конструктор и куча других полей
}
public class Client {
private String id;
private String username;
private String email;
private Boolean isActive = true;
@Autowired
private ClientService clientService;
public void printWelcomeMessage() {
System.out.println(String.format("Hello, %s!", username));
}
public void disableClient() {
clientService.disableClient(this.id);
this.isActive = false;
}
public void printClientStatus() {
System.out.println(this.isActive? "Active" : "Inactive");
}
public void sendEmail() {
clientService.sendEmailToClientWithId(this.email);
}
}
Answer the question
In order to leave comments, you need to log in
The second option from the point of view of OOP is definitely more correct. The answer to the question is yes, that's right.
In the first case, the main pitfall will be that you will need to implement a service in each entity.
Here in this book , everything is explained quite clearly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question