如何将这4个函数合二为一?

发布于 2024-11-19 11:07:08 字数 1432 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

横笛休吹塞上声 2024-11-26 11:07:08

您可以传入一个 lambda,定义要对每个元素执行的操作。

public bool ChangeSelectedRouterState(Action<Router> action) 
{
    for (int i = 0; i < m_listPlatforms.Count; i++)
    {
        if (m_listPlatforms[i].IsCheked)
            action(m_listPlatforms[i]);
    }
    return false;
}

像这样的调用:

ChangeSelectedRouterState(r => r.Stop());

需要将我为答案发明的类型 Router 替换为您正在处理的特定类型。

You can pass in a lambda defining the Action you want to do on each element.

Something like:

public bool ChangeSelectedRouterState(Action<Router> action) 
{
    for (int i = 0; i < m_listPlatforms.Count; i++)
    {
        if (m_listPlatforms[i].IsCheked)
            action(m_listPlatforms[i]);
    }
    return false;
}

Call like this:

ChangeSelectedRouterState(r => r.Stop());

You will need to substitute the type Router, which I invented for my answer, for your specific type you are working on.

捎一片雪花 2024-11-26 11:07:08

您可以通过将 Action 传递给执行所需操作的方法来使用策略模式(假设您的类称为 Platform) - 像这样:

    public bool ActionSelectedRouter(Action<Platform> action)
    {
        for (int i = 0; i < m_listPlatforms.Count; i++)
        {
            if (m_listPlatforms[i].IsCheked)
                action(m_listPlatforms[i]);
        }
        return false;            
    }

You could use the strategy pattern by passing in a Action to a method that performs the required action (assuming your class is called Platform) - so something like this:

    public bool ActionSelectedRouter(Action<Platform> action)
    {
        for (int i = 0; i < m_listPlatforms.Count; i++)
        {
            if (m_listPlatforms[i].IsCheked)
                action(m_listPlatforms[i]);
        }
        return false;            
    }
淡看悲欢离合 2024-11-26 11:07:08

您提取重复部分,循环,并传递您想要执行的操作:

public bool DoSelectedRouter(StatusRouter statusRouter)
{
    for (int i = 0; i < m_listPlatforms.Count; i++)
    {
        if (m_listPlatforms[i].IsCheked)
        {
            switch(statusRouter)
            {
               case StatusRouter.Stop:
                 m_listPlatforms[i].Stop();
                 break;
               case StatusRouter.Resume:
                 m_listPlatforms[i].Resume();
                 break;       
                 .......
            }
        }            
    }
    return false;
}

当然,您始终可以丢弃枚举并传递您想要执行的操作的函数委托。

You extract the repeating part, the loop, and pass the action you want to do:

public bool DoSelectedRouter(StatusRouter statusRouter)
{
    for (int i = 0; i < m_listPlatforms.Count; i++)
    {
        if (m_listPlatforms[i].IsCheked)
        {
            switch(statusRouter)
            {
               case StatusRouter.Stop:
                 m_listPlatforms[i].Stop();
                 break;
               case StatusRouter.Resume:
                 m_listPlatforms[i].Resume();
                 break;       
                 .......
            }
        }            
    }
    return false;
}

Of course, you can always discard the enum and pass a function delegate of action you want to do.

岁月苍老的讽刺 2024-11-26 11:07:08

首先,我将单个路由器的状态设置分解为一个单独的函数。我会让这个函数成为 Router 类的成员。

然后我会编写一个辅助函数来让我检查所有路由器。最后是一个在每个设备上调用 SetRouterStatus 函数的函数。

这样每个函数都有一个单一的职责并且可以重用。

您的代码还有一些其他问题:
Cheked 应为CheckedSuspect 应为Suspend

为什么你的函数返回的 bool 总是 false?

public void SetSelectedRoutersStatus(StatusRouter status)
{
    foreach(var router in GetCheckedRouters())
    {
        SetRouterStatus(router);
    }
}

IEnumerable<Router> GetCheckedRouters()
{
    return m_listPlatforms.Where(router=>router.Checked);
}

void SetRouterStatus(Router router,StatusRouter status)
{
     switch(status)
     {
         case(StatusRouter.Stop):
         {
                router.Stop();
                break;
         }
         case(StatusRouter.Start):
         {
                router.Start();
                break;
         }
         case(StatusRouter.Suspend):
         {
                router.Suspend();
                break;
         }
         case(StatusRouter.Resume):
         {
                router.Resume();
                break;
         }
         default:
           throw new NotSupportedException("Invalid StatusRouter");
     }
}

(基于@peer的回答)

First I'd factor out the status setting of a single router into a separate function. I'd make this function a member on the Router class.

Then I'd write a helper function getting me all checked routers. And finally a function that calls the SetRouterStatus function on each of them.

This way every function has a single responsibility and can be reused.

A few other issues with your code:
Cheked should be Checked and Suspect should the Suspend.

Why do your functions return a bool that's always false?

public void SetSelectedRoutersStatus(StatusRouter status)
{
    foreach(var router in GetCheckedRouters())
    {
        SetRouterStatus(router);
    }
}

IEnumerable<Router> GetCheckedRouters()
{
    return m_listPlatforms.Where(router=>router.Checked);
}

void SetRouterStatus(Router router,StatusRouter status)
{
     switch(status)
     {
         case(StatusRouter.Stop):
         {
                router.Stop();
                break;
         }
         case(StatusRouter.Start):
         {
                router.Start();
                break;
         }
         case(StatusRouter.Suspend):
         {
                router.Suspend();
                break;
         }
         case(StatusRouter.Resume):
         {
                router.Resume();
                break;
         }
         default:
           throw new NotSupportedException("Invalid StatusRouter");
     }
}

(based on @peer's answer)

梦行七里 2024-11-26 11:07:08

您可以将 DoAction 复制到函数中,但这更具可读性

public bool DoActionSelectedRouter(StatusRouter action)
{
    for (int i = 0; i < m_listPlatforms.Count; i++)
    {
        if (m_listPlatforms[i].IsCheked)
        {
            DoAction(m_listPlatforms[i], action);
        }
    }
    return false;
}

private void DoAction(Platform platform,StatusRouter  action)
{
     switch(action){
         case(StatusRouter.Stop):
         {
                platform.Stop();
                break;
         }
         case(StatusRouter.Start):
         {
                platform.Start();
                break;
         }
         case(StatusRouter.Suspend):
         {
                platform.Suspend();
                break;
         }
         case(StatusRouter.Resume):
         {
                platform.Resume();
                break;
         }
     }
}

You can copy DoAction into the function, but this is more readable

public bool DoActionSelectedRouter(StatusRouter action)
{
    for (int i = 0; i < m_listPlatforms.Count; i++)
    {
        if (m_listPlatforms[i].IsCheked)
        {
            DoAction(m_listPlatforms[i], action);
        }
    }
    return false;
}

private void DoAction(Platform platform,StatusRouter  action)
{
     switch(action){
         case(StatusRouter.Stop):
         {
                platform.Stop();
                break;
         }
         case(StatusRouter.Start):
         {
                platform.Start();
                break;
         }
         case(StatusRouter.Suspend):
         {
                platform.Suspend();
                break;
         }
         case(StatusRouter.Resume):
         {
                platform.Resume();
                break;
         }
     }
}
山人契 2024-11-26 11:07:08
Public bool StartRouterAction(StatusRouter Action)
{

switch(Action)
{
    case StatusRouter.Start:

// your start action

break; 

  case StatusRouter.Resume:

// your Resume action

  break; 

  case StatusRouter.Suspend:

  // your suspend action

       break; 


case StatusRouter.Stop:

 // your stop action

 break; 

}

}
Public bool StartRouterAction(StatusRouter Action)
{

switch(Action)
{
    case StatusRouter.Start:

// your start action

break; 

  case StatusRouter.Resume:

// your Resume action

  break; 

  case StatusRouter.Suspend:

  // your suspend action

       break; 


case StatusRouter.Stop:

 // your stop action

 break; 

}

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