Class Diagram methods are defined in the same class.

huangapple go评论63阅读模式
英文:

Why Class Diagram methods are defined in the same class?

问题

在类图表示中,字段和与类相关的方法会显示在图表上。然而,当将图表转换为类时,我看到这些方法会显示为接口方法,如下所示:

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();
}

这通常是正常使用的类实现吗?还是说这只是一个较高级别的实现?因为通常情况下,我会在该类的服务(AccountService)中实现addProduct(Product product)和其他方法。这样做会有问题吗?

英文:

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&lt;CreditCard&gt; 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?

答案1

得分: 0

UML是设计无关的。如果你定义一个类如下:

Class Diagram methods are defined in the same class.

它会被理解成你所展示的代码,而任何良好的代码生成器都会生成你所拥有的代码。

如果你想将领域类Account仅包含数据,并将领域逻辑分离到一个AccountService类中,这是你的权利,但你需要在UML中建模两个不同的类。这种设计方法被称为贫血领域模型,不管你是否喜欢,它被认为是一种反模式。

英文:

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

Class Diagram methods are defined in the same class.

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.

huangapple
  • 本文由 发表于 2023年2月24日 14:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定