javaspring-bootoopclass-diagramclass-design

Why Class Diagram methods are defined in the same class?


In class diagram representation, the fields and the class related methods are shown on the diagram. However, when converting the diagram to a class, I see that these methods are shown like an interface method as shown:

public class Account {

    private String userName;
    private String password;
    private String name;
    private String email;
    private String phone;
    private List<CreditCard> creditCards;

    public boolean addProduct(Product product);
    public boolean addProductReview(ProductReview review);
    public boolean resetPassword();
}

Is it the class implementation that is normally used? Or just an higher level implementation? Because normally, I would implement addProduct(Product product) and the other methods in the service of this class (AccountService). Is it wrong?


Solution

  • UML is design agnostic. If you define a class like:

    enter image description here

    It is understood like the code you've shown, and any decent code generator would generate the code you have.

    If you want to keep your domain class Account with only data and separate the domain logic into an AccountService class, it is your right, but you'll have to model two distinct classes in UML. This design approach is by the way called the anemic domain model (see also here), and, like it or not, it is considered as an anti-pattern.