Java pattern question - builder or db code in model?

I need to build a 'MyThingie' using 'A'. MyThingie is in the model package and currently no code in model accesses the DB. My question is which of the following patterns should I use? The top or bottom? Or something completely different. package com.model; public class MyThingie { private String foo = ""; private String bar = ""; private X x = null; private Y y = null; private Z z = null; public MyThingie() { } public MyThingie(A a, X x, Y y, Z z) { this.foo = a.getFoo(); this.bar = a.getBar(); this.x = x; this.y = y; this.z = z; } public static MyThingie createFromDb(A a) { X x = fetchXFromDB(a.getBlah()); Y y = fetchYFromDB(a.getFlah()); Z z = fetchZFromDb(a.getZlah()); return new MyThingie(a, x, y, z); } // getters and setters } // ----------- OR---------------- package com.model; public class MyThingie { private String foo = ""; private String bar = ""; private X x = null; private Y y = null; private Z z = null; public MyThingie() { } // getters and setters } package com.builder; public class MyThingieBuilder { public MyThingieBuilder() { } public static MyThingie createFromDb(A a) { MyThingie m = new MyThingie(); m.setFoo(a.getFoo()); m.setBar(a.getBar()); X x = fetchXFromDB(a.getBlah()); Y y = fetchYFromDB(a.getFlah()); Z z = fetchZFromDb(a.getZlah()); m.setX(x); m.setY(y); m.setZ(z); return m; } }
Both solutions are Ok. But there is a 'builder' methodology for this cases - the Builder is a inner class offering various build-options for the containing class (from the Effective Java Book).

以上就是Java pattern question - builder or db code in model?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏