我有一个类,它本质上是数据库中大型数据对象的包装器。看起来像这样:
public class ServerWrapper {
private DataObject object;
public ServerWrapper(DataObject object) {
this.object = object;
}
public void doAThing1() {
getSomeStuff();
// do stuff that modifies this object
}
public void doAThing2() {
getSomeStuff();
// do other stuff that modifies this object
}
private List<> getSomeStuff();
}
这就是问题所在:有很多很多人在做什么?方法。其中一些非常大。此外,他们中的很多人也在ServerWrapper中使用其他私有方法。理想情况下,我想将这些公共方法分解为自己的类,如ThingDoer1,ThingDoer2,但我不知道最好的方法。
这样的事情:
public class ThingDoer1{
public void doAThing1(ServerWrapper wrapper) {
wrapper.getSomeStuff();
// do the thing to wrapper
}
看起来非常臭;它与ServerWrapper紧密耦合(ServerWrapper调用它并调用ServerWrapper),而且它需要对它给出的对象(这是坏的)做一些事情,或者做一个副本,做这些事情,然后返回副本。
真的,我认为我正在寻找的是一组部分类,只是为了让这个类的怪物更容易管理;但是我使用的是Java,它并不支持。
是否有一些标准做法可以打破像这样的大班?提前谢谢!
编辑: 包装器的要点是将服务器端功能添加到数据库对象。例如,此对象需要&#34;已过期&#34;。这需要获取数据库表的所有关联,然后对对象和那些关联进行多次验证,然后在对象及其关联中设置一堆字段,然后在对象和所有这些关联上调用数据库更新。在ServerWrapper中包含所有代码对我来说很有意义,但是有一些相当复杂的操作需要发生,因此类本身变得相当大。
答案 0 :(得分:2)
但它并不需要与ServerWrapper
紧密结合:
public class ThingDoer1() {
public void doAThing1(List<> theList) {
// do the thing to object
}
然后在ServerWrapper
:
public void doAThing1() {
new ThingDoer1().doAThing1(getSomeStuff());
}
我可能会走得更远:
public class ThingDoer1() {
private final List<> theList;
public ThingDoer1(List<> theList) {
this.theList = theList;
}
public void doAThing() {
// do the thing to object
}
}
在ServerWrapper
:
public void doAThing1() {
new ThingDoer1(getSomeStuff()).doAThing();
}