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.
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;
}
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.
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;
}
}
发布评论
评论(6)
您可以传入一个 lambda,定义要对每个元素执行的操作。
您
像这样的调用:
需要将我为答案发明的类型
Router
替换为您正在处理的特定类型。You can pass in a lambda defining the Action you want to do on each element.
Something like:
Call like this:
You will need to substitute the type
Router
, which I invented for my answer, for your specific type you are working on.您可以通过将 Action 传递给执行所需操作的方法来使用策略模式(假设您的类称为 Platform) - 像这样:
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:
您提取重复部分,循环,并传递您想要执行的操作:
当然,您始终可以丢弃枚举并传递您想要执行的操作的函数委托。
You extract the repeating part, the loop, and pass the action you want to do:
Of course, you can always discard the enum and pass a function delegate of action you want to do.
首先,我将单个路由器的状态设置分解为一个单独的函数。我会让这个函数成为 Router 类的成员。
然后我会编写一个辅助函数来让我检查所有路由器。最后是一个在每个设备上调用
SetRouterStatus
函数的函数。这样每个函数都有一个单一的职责并且可以重用。
您的代码还有一些其他问题:
Cheked
应为Checked
,Suspect
应为Suspend
。为什么你的函数返回的
bool
总是 false?(基于@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 beChecked
andSuspect
should theSuspend
.Why do your functions return a
bool
that's always false?(based on @peer's answer)
您可以将 DoAction 复制到函数中,但这更具可读性
You can copy DoAction into the function, but this is more readable