“并不总是使用基类参数”代码气味
假设您有这样的代码:
public Base
{
abstract void Register();
}
public Registrator1: Base
{
override void Register()
{
//uses the current state of the object to populate the UI captions
}
}
public Registrator2: Base
{
override void Register()
{
//uses the current state of the object to populate the UI captions
}
}
但是当您收到一个新的业务规则要求您编写 Registrator3,它实际上根据某个参数进行注册,并且您将代码库更改为下一个:
public Base
{
abstract void Register(externalParam);
}
public Registrator1: Base
{
override void Register(externalParam)
{
//uses the current state of the object to populate theUI
}
}
public Registrator2: Base
{
override void Register(externalParam)
{
//uses the current state of the object to populate the UI
}
}
public Registrator3: Base
{
override void Register(externalParam)
{
//uses a DDD - service passed in the params to populate the UI
}
}
但是 Registrator1 和 Registrator2 不需要该参数,并且代码变得很臭。有哪些方法可以重写这段代码?
Suppose you had such code:
public Base
{
abstract void Register();
}
public Registrator1: Base
{
override void Register()
{
//uses the current state of the object to populate the UI captions
}
}
public Registrator2: Base
{
override void Register()
{
//uses the current state of the object to populate the UI captions
}
}
But When you receive a new business rule asking you to write Registrator3 which actually registers based on some parameter and you change your code base to the next:
public Base
{
abstract void Register(externalParam);
}
public Registrator1: Base
{
override void Register(externalParam)
{
//uses the current state of the object to populate theUI
}
}
public Registrator2: Base
{
override void Register(externalParam)
{
//uses the current state of the object to populate the UI
}
}
public Registrator3: Base
{
override void Register(externalParam)
{
//uses a DDD - service passed in the params to populate the UI
}
}
But Registrator1 and Registrator2 do not need that param and the code becomes smelly. What are the ways to re-write this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在这里使用对象作为参数;这通常用于参数数量可能根据所使用的调用而变化的场景。
这样做的优点是您不需要在每次添加参数时更改方法参数(这会破坏接口)。用法也变得有点自我记录:
它就像空气清新剂,用于这种类型的代码气味,虽然它仍然有臭味; 最后,您
可以通过将其设置为
params
参数来使调用站点更干净(这会产生少量开销);老实说,虽然它更臭,因为它是一种语言黑客。最后你可以用泛型来改进它:You could use an object as a parameter here; which is commonly used in scenarios where the number of parameters can vary depending on the call being used.
This has the advantage that you don't need to change method parameters (which is breaking interface) each time a parameter is added. The usage also becomes somewhat self-documenting:
It's like air freshener for this type of code smell, while it's still smelly; you can make it smell nicer.
Finally you can make the call-site cleaner by making it a
params
argument (this has a small amount of overhead); in all honesty though it is more smelly because it's a language hack. Finally you could improve it with generics:是否不可能在Registrator3中包含externalParam的逻辑?
换句话说,Registrator3使用参数,然后调用未修改的无参数基数?
很大程度上取决于逻辑所属的位置。如果它是基类固有的东西,则将其放入基类中,并重载 Register() 函数或为参数提供默认值,以便子类不需要提供它。
Is it not possible to contain the logic for externalParam in Registrator3?
In other words, Registrator3 uses the param, then calls the unmodified parameterless base?
A lot really depends on where the logic belongs. If it is something intrinsic to the base, then put it in the base, and either overload the Register() function or supply a default value for the param so that sub classes don't need to provide it.
假设您想重用基类中的注册逻辑,您可以按如下方式更新代码:
HTH,
Cosmin
编辑:更新了要编译的代码。
Assuming you want to reuse the registration logic from the base class, you could update the code as follows:
HTH,
Cosmin
EDIT: Updated code to compile.