我有3个类:服务,客户端和混乱(我应该以某种方式重构)。如何将变量关联到对象并返回它们?
Class Service {
void servicemethod(int a, int b, int c)
{
//does something with params a, b and c
}
}
我这就要求从相同的混乱两种方法的客户端类
Class Client {
main()
{
Mess.setABC(x)
Mess.callintermediatemethod(int x)
}
}
残局类什么也不做,但读PARAMS A,B和C,这与键“X”相关的值在一个文件中,并且调用Service.servicemethod(a, b, c)
:
Class Mess {
static void setABC(int x)
{
//sets static params a, b and c with values read from json file that has x as its key
}
void callIntermediatemethod(x)
{
Service.servicemethod(a, b, c);
//here x parameter is not even necessary since it already knows what A, B and C are.
}
}
很明显,Mess类做了两件事。在读取x之后设置静态变量,并使用存储的值调用service方法,并使用伪参数。这显然是糟糕的设计。但是,如果我决定消除混乱,如何设置参数a,b,c给定变量x?我应该将它们与对象关联吗?装饰他们?使用构建器模式还是依赖注入?如何清理混乱并让客户端类直接调用服务方法而不必通过Mess?
除非您有一个令人信服的理由证明静态需求,否则您应该更喜欢使用非静态方法和类实例变量。一般来说,你对Mess课程有什么不好的设计。花一些时间阅读并利用面向对象的设计。 – pczeus