我有三个课程:ToBeMum
,NewMum
和ExperiencedMums
。这些类有不同的方法,它们做同样的事情,但它们的参数不同。如何重构代码以避免复制/粘贴所有三个类?例如:
NewMum:
public List<Question> GetMotherActualDayCareAge()
{
List<QuestionInfo> mumsHabits;
var answers = Answers.GetAnswers;
string answerValue = string.Empty;
var oneA = from a in answers
where a.Questionid == "Q49d"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var oneB = from a in answers
where a.Questionid == "Q65d"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var temp = oneA.Union(oneB).ToList();
mumsHabits = temp.Intersect(mothers, new UserIdEqualityComparer()).ToList();
return mumsHabits;
}
ExperiencedMum
public List<QuestionInfo> GetMotherActualDayCareAge()
{
List<QuestionInfo> mumsHabits;
var answers = Answers.GetAnswers;
string answerValue = string.Empty;
var oneA = from a in answers
where a.Questionid == "Q49c"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var oneB = from a in answers
where a.Questionid == "Q65c"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var temp = oneA.Union(oneB).ToList();
mumsHabits = temp.Intersect(mothers, new UserIdEqualityComparer()).ToList();
return mumsHabits;
}
答案 0 :(得分:1)
这将非常简单。您可以在单独的类中使用相同的概念编写一个通用方法。
例如,创建一个名为Helper.cs的类,该类应包含以下方法
public List<QuestionInfo> GetMotherActualDayCareAge(string var1, string var2)
{
List<QuestionInfo> mumsHabits;
var answers = Answers.GetAnswers;
string answerValue = string.Empty;
var oneA = from a in answers
where a.Questionid == var1
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var oneB = from a in answers
where a.Questionid == var2
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var temp = oneA.Union(oneB).ToList();
mumsHabits = temp.Intersect(mothers, new UserIdEqualityComparer()).ToList();
return mumsHabits;
}
现在只需在需要的地方调用此方法即可。在您的情况下,您可以从ToBeMum.cs,NewMum.cs和ExperiencedMums.cs调用此方法。
对于ex,在NewMum.cs中,你可以像这样打电话。
List<QuestionInfo> quesList = GetMotherActualDayCareAge("Q49d", "Q65d");
在ExperiencedMum.cs中,你可以像这样打电话。
List<QuestionInfo> quesList = GetMotherActualDayCareAge("Q49c", "Q65c");
所以现在这变得动态,只写了一个方法,并且多次调用该方法而没有复制/粘贴。
答案 1 :(得分:1)
这是user2549110第二个问题的答案。
尝试创建一个像AbsMumSurvey.cs这样的抽象类,它应该包含以下内容,
public abstract class AbsMumSurvey
{
public abstract void IsDayCare();
public abstract void IsEconomicalToEducateChild();
public int NoOfBabies()
{
return 2;
//This is the sample logic just for understanding. You can write your own logic.
}
}
现在IsDayCare()和IsEconomicalToEducateChild()是应该在派生类中强制重写的抽象方法。
现在在GoingToDeliver.cs中,您可以执行以下操作。
public class GoingToDeliver : AbsMumSurvey
{
public override void IsDayCare()
{
//Write your logic here.
}
public override void IsEconomicalToEducateChild()
{
//Write your logic here.
}
}
您可能会也可能不会实施NoOfbabaies()。如果您还想实现NoOfBabies(),请尝试以下代码。
public new int NoOfBabies()
{
return base.NoOfBabies();
}
同样,您可以从许多类派生这些方法。
答案 2 :(得分:0)
我将提取以下常用方法:
var oneA = from a in answers
where a.Questionid == "Q49d"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var oneB = from a in answers
where a.Questionid == "Q65d"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
并将QuestionIds作为参数传递,而不是在代码中对它们进行硬编码。然后可以从两个现有方法调用此新方法,并避免重复代码