المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Refactoring Question about the same operations?



C# Programming
01-25-2010, 01:10 PM
I have a refactoring question. What do you do when you have a group of different classes or types that need the same operations performed on them. I have a code sample of something to illustrate my point. The code is the same except for the use of different classes internally. The classes have different properties but the same methods but the signatures are different because of the using generics. The classes derive for a common base class, but that has not helped as much as I wanted it to.

Could someone help me with a way to refactor the code so that the method exist 1 time in the class but I can use it with different classes?

I am looking for help and ideas on how to do it not just someone to change the code.


public class CBProcessor
{

string user = string.Empty;
string path = string.Empty;

public CBProcessor(string i_User, string i_RestrictedPath)
{
this.user = i_User;
this.path = i_RestrictedPath;
}

public string GetCBPrograms()
{
string returnString = string.Empty;
CBUserFilter uf = new CBUserFilter(this.user);
CBProgramFilter pf = new CBProgramFilter(this.path);
List bList = null;
List pList = new List();
if (uf.IsValid())
{
pList = CBPrograms.getInstance().GetData();
bList = CBPrograms.ToCBBase(pList);
List xlist = pf.IncludeOnlyList(bList);
returnString = CBPrograms.ToXml(xlist);
}

return returnString;
}

public string GetCBSummary()
{
string returnString = string.Empty;
CBUserFilter uf = new CBUserFilter(this.user);
CBProgramFilter pf = new CBProgramFilter(this.path);
List bList = null;
List sList = new List();
if (uf.IsValid())
{
sList = CBSummary.getInstance().GetData();
bList = CBSummary.ToCBBase(sList);
List xlist = pf.IncludeOnlyList(bList);
returnString = CBSummary.ToXml(xlist);
}

return returnString;
}

public string GetCBEntries(string i_ProgramName)
{
string returnString = string.Empty;
CBUserFilter uf = new CBUserFilter(this.user);
CBProgramFilter pf = new CBProgramFilter(this.path);
List bList = null;
List rList = new List();
if (uf.IsValid())
{
rList = CBEntries.getInstance().GetData(i_ProgramName);
bList = CBEntries.ToCBBase(rList);
List xlist = pf.IncludeOnlyList(bList);
returnString = CBEntries.ToXml(xlist);
}

return returnString;
}


}


Thank you for you time!