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条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况