我可以使用通用的最佳实践方法吗? [英] Can I use generic best practice to the methods?

查看:76
本文介绍了我可以使用通用的最佳实践方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四种方法,它们非常相似。

Hi, I have the four methods, they are pretty similar.

private void MovePieceToUpperStation(Button currentButton, string rowId, string colId)
       {
           if (rowId == "8")
               return;
           var nextButtonName = ButtonNameNextToCurrentOneGetter.GetButtonNameOfTheUpperStation(colId, rowId);
           MovePieceToNextStation(currentButton, nextButtonName);
       }

       private void MovePieceToLowerStation(Button currentButton, string rowId, string colId)
       {
           if (rowId == "1")
               return;
           var nextButtonName = ButtonNameNextToCurrentOneGetter.GetButtonNameOfTheLowerStation(colId, rowId);
           MovePieceToNextStation(currentButton, nextButtonName);
       }

       private void MovePieceToRightStation(Button currentButton, string rowId, string colId)
       {
           if (colId == "H")
               return;
           var nextButtonName = ButtonNameNextToCurrentOneGetter.GetButtonNameOfTheRightStation(rowId, colId);
           MovePieceToNextStation(currentButton, nextButtonName);
       }

       private void MovePieceToLeftStation(Button currentButton, string rowId, string colId)
       {
           if (colId == "A")
               return;
           var nextButtonName = ButtonNameNextToCurrentOneGetter.GetButtonNameOfTheLeftStation(rowId, colId);
           MovePieceToNextStation(currentButton, nextButtonName);
       }





我的问题是我不想重复它们。有没有一个很好的方法呢?

例如,使用泛型。



我尝试过:



可以将另一个参数传递给方法吗?



My question is I do not want repeat them. Is there a good way to do that?
For example, using generic.

What I have tried:

May pass another parameter to the method?

推荐答案

不容易 - 他们会做不同的事情看起来很相似。

你需要传递三个额外的参数:比较id(rowId或ColId),比较值(8,1,H 或A)以及执行方法的代表(GetButtonNameOfTheUpperStation,GetButtonNameOfTheLowerStation,GetButtonNameOfTheRightStation或GetButtonNameOfTheLeftStation)

这意味着每次调用都必须知道将要发生的事情的详细信息,以及为什么要调用常见方法 - 这很麻烦,容易出错。



我不会这样做。
Not easily - they do different things that just look similar.
You'd need to pass three extra parameters: the "comparison id" (either rowId or ColId), the "compare value" ("8", "1", "H", or "A") and a delegate for the method to execute (GetButtonNameOfTheUpperStation, GetButtonNameOfTheLowerStation, GetButtonNameOfTheRightStation, or GetButtonNameOfTheLeftStation)
And that means that every call has to know the details of what is going to happen, and why in order to call the "common" method - that's messy and prone to error.

I wouldn't do it.

你好,



这里没有太多简化。

你在每个方法中调用不同的方法。这不是泛型可以帮助你的东西。
Hello,

there's not much to simplify here.
You are calling a different method in each of them. That's not something that generic can help you with.


这些方法只在结构上相似,而不是在功能上。问题是每种方法处理不同的数据并根据对该数据施加的不同条件执行不同的代码。



没有办法将该代码压缩为单一方法以任何优雅的方式。
Those methods are only similar in structure, not in function. The problem is each method deals with different data and executes different code depending on different conditions imposed on that data.

There's no way to condense that code down to a single method in any elegant manner.


这篇关于我可以使用通用的最佳实践方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆