我很难尝试实现关于循环内条件的SOLID原则。 例如,我有一个对象列表。
public class ClassTwo{
int id;
string name;
string quantity;
bool markasDelete;
}
public class SampleClass{
string name;
List<ClassTwo> listClassTwo;
///other initializations, props, method ,construct
void Process(List<ClassTwo> newListOfClassTwo){
foreach(ClassTwo c in newListOfClassTwo){
if(c.id == 0)
{
///do other things
listClassTwo.Add(c);
}
else if(c.id > 0 && !markasDelete)
{
///do update things
}
else
{
///do delete things
}
}
}
}
从上方可以看到,我的“ Process”方法做得太多。我试图将将每个条件细分为特定方法的工作减少,但对此我非常不满意。我的代码现在看起来像这样。
void Process(List<ClassTwo> newListOfClassTwo){
foreach(ClassTwo c in newListOfClassTwo){
AddMethod(c);
UpdateMethod(c);
RemoveMethod(c);
}
}
请有人帮我吗?