liput 2009-03-21 12:37
浏览 227
已采纳

从User POJO中再抽取出AbstractUser有什么优势?

记不起来是哪次看到有一个这样子的讲法,从POJO中再进行一次抽象,如从User中再使用AbstractUser,这样子做有什么好处呢?是为了序列化的应用吗?
今天在MyEclipse的生成代码中又一次看到了,请帮忙解答一下。
[b]问题补充:[/b]
如果这种抽象操作是一对一(一个POJO对应两个类,***和Abstract***)的关系呢?这种情况有没有可能哦?

[b]问题补充:[/b]
很谢谢各位,我记起来了,那次书上是说为了代码生成器而产生的,好像是为了代码生成器不会重复覆盖修改的POJO而进行的,是在《Pro Web 2.0 Application Development with GWT》一书中,原文如下:
[code="java"]
One additional best practice that I like to maintain is to keep all persisted properties of a class in an abstract class in their own package. This practice is almost certainly necessary if you plan to use any code generation tools, but even if you don’t, the separation
of a class’s custom business methods from its properties and getter/setter methods is still a nice habit to get into and will leave us with cleaner code. The practice is pretty much mandatory for code generation tools, because most of these tools will simply overwrite
whatever objects they’re in the process of creating. That means that if you edit the generated files, you’ll lose all your changes. Storing the bean properties and their getters and setters in an abstract class will help us avoid this problem and will clean up our objects to boot.
[/code]
意思就是将业务逻辑写在子类中,这样当需要重复修改域模型的话,就不会因为被代码生成器覆盖掉写的业务逻辑了。

  • 写回答

6条回答

  • xenocide 2009-03-21 14:32
    关注

    我觉得这是代码生成器的作者为了省事而产生的……

    譬如说有可能你会让它生成AUser和BUser,使用AbstractUser无可厚非

    但是如果你只生成User,那么生成器套用生成多个类的模版就行了。
    除了多出个AbstractUser,没多少不同……

    当然也有些别的好处:

    譬如你只改User,MyEclipse只改AbstractUser,楚河汉界清清楚楚,你也不用对着一大堆生成的代码损耗视力……

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料