记不起来是哪次看到有一个这样子的讲法,从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]
意思就是将业务逻辑写在子类中,这样当需要重复修改域模型的话,就不会因为被代码生成器覆盖掉写的业务逻辑了。