是否有用于编写这样的代码的反模式名称?
下面是一些使用参数类来包含 Show()
方法的可能参数的代码。此 FooOption
类中的值不是很相关。您可以通过查看下面的 Show()
实现来了解这一点。我知道这是糟糕的代码,但是是否有与此相关的反模式?
class FooOptions {
public int? Id { get; set; }
public string BazContext { get; set; }
public int? BazId { get; set; }
}
class BarMgr {
public Bar Show(FooOptions options) {
if (options == null)
options = new FooOptions();
if (options.Id.HasValue)
return svc.GetBar(options.Id.Value);
if (!string.IsNullOrEmpty(options.BazContext) && options.BazId.HasValue)
return svc.GetBar(options.BazContext, options.BazId.Value);
return null;
}
}
更新: 我知道参数对象不是反模式。根据我的经验,参数对象属性是相关的。这就是我试图找到的可能的反模式。设置所有三个属性是没有意义的。
Here is some code that uses a parameter class to contain the possible parameters to the Show()
method. The values in this FooOption
class aren't very related. You can see this by looking at the implementation of Show()
below. I know this is bad code, but are there any anti-patterns related to doing this?
class FooOptions {
public int? Id { get; set; }
public string BazContext { get; set; }
public int? BazId { get; set; }
}
class BarMgr {
public Bar Show(FooOptions options) {
if (options == null)
options = new FooOptions();
if (options.Id.HasValue)
return svc.GetBar(options.Id.Value);
if (!string.IsNullOrEmpty(options.BazContext) && options.BazId.HasValue)
return svc.GetBar(options.BazContext, options.BazId.Value);
return null;
}
}
Update:
I know that parameter objects are not an anti-pattern. In my experience, parameter object properties are related. This is the possible anti-pattern that I am trying to locate. setting all three properties makes no sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新后,这是我的回答:
据我所知,像这样的反模式没有真正的名字,但这种方法至少违反了一个原则:
单一责任原则。
这确实是方法的问题,而不是参数对象的问题。
After your update, here my answer:
As far as I know, there is no real name for an anti-pattern like this, but there is at least one principle that this method violates:
The Single-Responsibility-Principle.
And it really is a problem of the method and not of the parameter object.
它被称为参数对象模式,并且不被视为反模式——这是处理否则会有太多参数的方法的好方法。
It's called the parameter object pattern, and it's not considered an antipattern -- it's a good way to deal with methods that would otherwise have too many parameters.
如果您经常使用选项,则可能会出现反模式,我们有一种称为功能嫉妒的东西,这表明您可能希望将功能转移到正在使用的实际功能中。
There might be an anti-pattern if you use options a lot we have something called feature envy and is an indication that you might want to move functionality into the actual feature being used.