简体   繁体   English

通过“程序到接口”,如何良好的实践以及POJO中的瞬时对象/休眠对象中的模型如何?

[英]How good practice and how is Transient Objects in POJO / Models in Hibernate Objects through “Program To Interface”?

I am working on basic financial/banking projects. 我正在从事基本的金融/银行项目。 There is "Money" with different contexts, not only currency type texts but also I need other manipulations. 存在具有不同上下文的“货币”,不仅货币类型文本,而且我还需要其他操作。

In DB, I persist the amount of money as NUMBER field, and in Hibernate as BigDecimal type object. 在DB中,我将金额保留为NUMBER字段,在Hibernate中为BigDecimal类型的对象。 Let's say Transfer is an object mapped to a table which has the information of money transfer between two people; 假设Transfer是一个映射到表的对象,该表具有两个人之间的转账信息;

@Entity
class Transfer {
    @Id @Column
    private Long id;

    @Column
    private Person from; //mapped personId

    @Column
    private Person to; //mapped personId

    @Column
    private BigDecimal amount;

    //... setters & getters 
}

But actually; 但实际上; almost always I need the amount as Money and with special toString() method: asText() , not only the BigDecimal object and because I need this exactly: 几乎总是我需要用Money作为金额,并且需要特殊的toString()方法:asText(),不仅是BigDecimal对象,而且因为我确实需要这个:

Transfer t = dao.getMySpecialTransferObject();
System.out.println(t.money.asText());

But I still need the amount directly, because I need to do some calculations on the amount of money. 但是我仍然直接需要金额,因为我需要对金额进行一些计算。 (And I need Money type objects in other Entities as well) (我也需要其他实体中的Money类型对象)

Now I guess I need to create such a class; 现在我想我需要创建一个此类。

class Money {
    private BigDecimal amount;
    //... setters & getters

    public String asText(){
        String textToReturn = "The amount of this money is: " + amount.toString;
        // ... and some locale information and manipulation on textToReturn
        return textToReturn;
    }
}

As complexicity grows, Design Patterns say "program to interface", then I think I will do this Money as interface and an implementation; 随着复杂性的增长,设计模式会说“程序到接口”,那么我想我将把这笔钱作为接口和实现来使用;

interface Money{
    public String asText();
}

class BasicMoney implement Money{
    private BigDecimal amount;
    //... setters & getters

    public String asText(){
        String textToReturn = "The amount of this money is: " + amount.toString();
        // ... and some locale information and manipulation on textToReturn
        return textToReturn;
}

So I want to edit my POJO "Transfer" with "Money" type object not as amount. 所以我想用“ Money”类型的对象来编辑我的POJO“ Transfer”而不是数量。

@Entity
class Transfer {
    @Id @Column
    private Long id;

    @Column
    private Person from; //mapped personId

    @Column
    private Person to; //mapped personId

    @Column
    // Not anymore: private BigDecimal amount;
    private Money money;

    //... setters & getters 
}

Alright, now here is the question: 好了,现在是这个问题:

My POJO "Transfer" would have same "Money" type object but what about DB relations? 我的POJO“ Transfer”将具有相同的“ Money”类型对象,但数据库关系如何? Money is not a persistent object in DB, so I might use @Transient annotation but I still need a coupling/relation between Money and the "amount" column in DB . Money不是DB中的持久对象,因此我可以使用@Transient批注,但是我仍然需要Money和DB中“ amount”列之间的耦合/关系 How should I keep this relation on POJO? 我应该如何在POJO上保持这种关系?

Note; 注意; We are doing this formatting etc. with util classes (without interfaces or transient objects) but I am not sure about this if this is the best practice and I don't want to develop util classes anymore unless there is no better solution. 我们正在使用util类(没有接口或临时对象)进行这种格式化等操作,但是我不确定这是否是最佳实践,除非有更好的解决方案,否则我将不再开发util类。

Keep your field as BigDecimal and just add helper class with static function which formats/displays your amount of money. 保持您的字段为BigDecimal,只需添加带有格式化/显示您的金额的静态函数的帮助器类即可。 In this case extracting Money interface doesn't make sense, because you have only "toString" method. 在这种情况下,提取Money接口没有任何意义,因为您只有“ toString”方法。 Keep in mind the KISS principle. 请记住KISS原则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM