这个图案叫什么名字?
我正在审查团队代码库中的一些代码,其中我们遍历一个分层数据结构并从中构建一个新的数据结构。没有嵌套循环——层次结构的每个级别都有其自己的专用函数。
所以我们有这样的代码:
public void DoA(A a, Transform transform)
{
foreach(B b in a)
DoB(b, transform);
}
public void DoB(B b, Transform transform)
{
if (b != null && b.IsAvailable)
return;
foreach(C c in b)
DoC(c, transform)
}
public void DoC(C c, Transform transform)
{
var cAndAHalf = DoCAndAHalf(c.FindAll(x => x.Children > 0);
foreach(D d in cAndAHalf)
DoD(d, transform);
}
. . .
public void DoX(X x, Transform transform)
{
Res res = new Res();
if (x.Selected)
{
res.Selected = true;
res.ResourceCount = 1;
}
transform.Add(res);
}
这样的方法有几十个,每个方法的长度都是 3 到 5 行,名称类似,通常包含一个简单的 null 检查或过滤器,粗略地查看代码会发现没有方法是确实调用了不止一次。出于单元测试目的,方法是公开的。
我个人发现代码很难浏览,因为数十个公共方法==数十个进入类内部的潜在入口点。
这种编码模式有名字吗?这是一种反模式吗?这种风格比简单地将循环嵌套在单个函数中更有利吗?
Some I'm reviewing some code within my team's code base, where we traverse over one hierarchical data structure and build a new data structure from it. There are no nested loops -- every level of the hierarchy has its own dedicated function.
So we have code like this:
public void DoA(A a, Transform transform)
{
foreach(B b in a)
DoB(b, transform);
}
public void DoB(B b, Transform transform)
{
if (b != null && b.IsAvailable)
return;
foreach(C c in b)
DoC(c, transform)
}
public void DoC(C c, Transform transform)
{
var cAndAHalf = DoCAndAHalf(c.FindAll(x => x.Children > 0);
foreach(D d in cAndAHalf)
DoD(d, transform);
}
. . .
public void DoX(X x, Transform transform)
{
Res res = new Res();
if (x.Selected)
{
res.Selected = true;
res.ResourceCount = 1;
}
transform.Add(res);
}
There are dozens of methods like this, where each method is 3 to 5 lines in length, similarly named, usually contains a trivial null check or filter, and a cursory review of the code shows that no method is really invoked more than once. Methods are public for unit testing purposes.
I personally find it the code very hard to navigate through, because dozens of public methods == dozens of potential entry points into the class guts.
Does this sort of coding pattern have a name? Is it an anti-pattern? Is this style more advantageous than simply nesting the loops in a single function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对我来说看起来像是责任链。您只需处理部分传入请求,然后转移到链中的下一个项目。
责任链
在您的情况下,它应该如下所示:
Looks like Chain-of-Repsonisbility to me. You just process part of the incoming request and then transfer to next item in the chain.
Chain-of-responsibility
In your case it should look like this:
哎呀。这绝对应该算是某种东西。但不确定这是一种反模式。我会选择反递归。
它也可能是按排列编程的标志(开发人员向层次结构添加一个级别每次结构变得更深)。
Yikes. That should most definitely qualify as something. Not sure it's an anti-pattern though. I'd go with anti-recursion.
It could also be a sign of Programming by Permutation (with the developer adding a level to the hierarchy each time the structure grows deeper).
我认为这是良好的编码风格——具有单一职责的小方法。如果方法命名得当,那么代码应该易于理解和维护。
当然,如果许多方法非常相似,则可以寻找一种通用模式,并通过使用委托或类似的方法将其分解出来 - 但这实际上取决于实际的代码。
This is something I would consider good coding style - small methods with a single responsibility. If the methods are well named this should yield code easy to understand and maintain.
Of course if many of the methods are very similar one could look for a common pattern and factor this out by using delegates or something similar - but that really depends on the actual code.
它有点像装饰器模式,但也只是这样。
http://en.wikipedia.org/wiki/Decorator_pattern
查一下看看是不是更具体地匹配您的代码库。
It kind of seems like the decorator pattern, but only kind of.
http://en.wikipedia.org/wiki/Decorator_pattern
Look it up and see if it matches your codebase more specifically.
这看起来有点像复合模式,只不过你可以利用父母的相似性以及分层结构中的子级,以最大程度地减少编码。
您可以通过让结构中的每个元素实现一个知道如何处理其子元素的接口来利用这一点,例如
This looks a bit like Composite pattern, except that there you can take advantage of the similarity of parents and children in a hierarchical structure to minimize your coding.
You might be able to take advantage of this by having each element in the structure implement an interface that knows how to process its children, e.g.