java model 类设计问题
黄舟
黄舟 2017-04-18 09:26:03
0
4
409

最近遇到项目 最基本的model 类设计是这样的
model层

public class ContractModel extends BaseModel { @Transient private static final long serialVersionUID = -7915586025811092910L; private Integer templatecategary; // //类型(1维保/2维修)是否有偿(1有偿/2无偿)形式(1**加粗文字**收款/2付款) private String name; //合同名称 private String code; //合同编码 }

但是后面又继承了这个model层 不知道这样设计有什么好处

public class ContractQueryModel extends **ContractModel**{ public String toString(){ return "Model"+this.getClass().getName()+","+super.toString()+" ,[]"; } }
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all (4)
Ty80

Just look at the name and make the following guesses:
ContractModelis a DTO, responsible for transmitting data;ContractModel是一个DTO,负责传输数据;
ContractQueryModel是一个查询参数对象,负责接收从ControllerContractQueryModelis a query parameter object, responsible for receiving fromControllerQuery parameters passed from (or other places);
Since they are all querying Contract, the fields are basically the same, but for the convenience of future expansion, inheritance is used here to implement it.

But in actual applications, DTO and query parameters are generally not the same structure. DTO is as shown in the title of the question, and query parameters are generally the plural form of the DTO field, for example:

public class ContractQueryModel implements Serializable { private Set templateCategaries = Collections.emptySet(); private Set names = Collections.emptySet(); private Set codes = Collections.emptySet(); ... }

In addition, @BodhiXuguang mentioned theRichter replacement principle, you can take a look at this, it mentions possible problems with the design of the subject.

    PHPzhong

    Not sure what your question is: "The meaning of inheritance"? Or "Why does ContractQueryModel only override thetoStringmethod"?

    What if you don’t understand the “meaning of inheritance”? Then I think the most basic understanding is for reuse. You can read the documentation:

    Document address: Inheritance

    If your doubt is, why did you inherit it for a long time and only rewrite thetoStringmethod in the end?

    I think the main reason is because the author wants to satisfy the opening and closing principle and wants a newtoString方法,又不想对原代码ContractModelmethod, but does not want to modify the original codeContractModel. In any case, you should discuss the details directly with the author, who may have deeper considerations. I can't tell much more from your code snippet.

    Added:

    I personally understand that this is not an "agency model" because it does not meet the basic characteristics of the "agency model". Regarding the agency model, see the following diagram:

    From this we know that in proxy mode, the proxy class should implement the same interface as the proxy class. TheContractQueryModelhere does not have this feature.

    Finally, I would like to talk about a superficial understanding of design patterns. The so-called design pattern is a best practice paradigm for solving specific problems. Some patterns have fixed characteristics and some do not. Therefore, based on just a few simple classes of the subject, it is difficult for me to judge which design pattern he is without the context (I mean those that do not have fixed characteristics).

      左手右手慢动作

      If you only override a toString method and use inheritance, the design itself is problematic.
      If your guess is correct, ContractModel is a model used in the persistence layer, and ContractQueryModel is a model used in the business layer. For convenience, a certain developer directly made the two into an inheritance relationship.
      Personally, I am very disgusted with this model of separation of data and logic. I prefer the following method:

      Contract contract = new Contract(); contract.setName(...) ... contract.save(); ... Contract other = Contract.find(...); ...
        黄舟

        You guys are so funny, I don’t even know what I want to ask, but you still answer it, and this question has become the recommendation of the week, it’s weird

          Latest Downloads
          More>
          Web Effects
          Website Source Code
          Website Materials
          Front End Template
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!