这个图案叫什么名字?

发布于 2024-09-11 07:09:48 字数 971 浏览 4 评论 0原文

我正在审查团队代码库中的一些代码,其中我们遍历一个分层数据结构并从中构建一个新的数据结构。没有嵌套循环——层次结构的每个级别都有其自己的专用函数。

所以我们有这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鱼窥荷 2024-09-18 07:09:49

对我来说看起来像是责任链。您只需处理部分传入请求,然后转移到链中的下一个项目。

责任链

在您的情况下,它应该如下所示:

Action action = new Action;
action = action.SetNext(DoA);
action = action.SetNext(DoB);
action = action.SetNext(DoC);

A a = new A();
action.Process(a);

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:

Action action = new Action;
action = action.SetNext(DoA);
action = action.SetNext(DoB);
action = action.SetNext(DoC);

A a = new A();
action.Process(a);
你与昨日 2024-09-18 07:09:49

哎呀。这绝对应该算是某种东西。但不确定这是一种反模式。我会选择反递归。

它也可能是按排列编程的标志(开发人员向层次结构添加一个级别每次结构变得更深)。

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).

内心激荡 2024-09-18 07:09:49

我认为这是良好的编码风格——具有单一职责的小方法。如果方法命名得当,那么代码应该易于理解和维护。

当然,如果许多方法非常相似,则可以寻找一种通用模式,并通过使用委托或类似的方法将其分解出来 - 但这实际上取决于实际的代码。

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.

迷爱 2024-09-18 07:09:49

它有点像装饰器模式,但也只是这样。

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.

天生の放荡 2024-09-18 07:09:49

这看起来有点像复合模式,只不过你可以利用父母的相似性以及分层结构中的子级,以最大程度地减少编码。

您可以通过让结构中的每个元素实现一个知道如何处理其子元素的接口来利用这一点,例如

public interface ChildProcessor { 
   public void process()
}
public A implements ChildProcessor { 
  public void process() { foreach (B b )... }
}
public B implements ChildProcessor { 
  public void process() { foreach (C c )... }
}

public void DoA(A a, Transform transform)
{
   A.process()... 
}

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.

public interface ChildProcessor { 
   public void process()
}
public A implements ChildProcessor { 
  public void process() { foreach (B b )... }
}
public B implements ChildProcessor { 
  public void process() { foreach (C c )... }
}

public void DoA(A a, Transform transform)
{
   A.process()... 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文