I have a doubt about good OOP design in this case:
The example is not real. But I can´t give you the real example because its private code. However the example concept it's exactly the same.
Imagine I have a class where I store a List of strings and I have a method called ThereIsString(string mystring).
I return true or false depending on if a computation I do with that string "is related" to one of the strings is listOfString. (this is the private algorithm)
Example:
public class StringBagAlgorithm()
{
List<string> listofString = new List<string>();
public boolean ComputeString(string myString)
{
return true or false depending on the computation with the list of strings;
}
}
Ok. the list of Strings is stored in a different class called ListOfStrings which has a reference to StringBagAlgorithm so:
public class ListOfStrings()
{
List<string> listofString = new List<string>();
List<string> MySecondListofString = new List<string>();
StringBagAlgorithm _bagAlgorithm
public ListOfStrings(StringBagAlgorithm bagAlgorithm)
{
this._bagAlgorithm = bagAlgorithm;
}
public void ComputeSecondList()
{
for (int i=0; i<MySecondListofString; i++ )
_bagAlgorithm.ComputeString(MySecondListofString[i]);
}
}
My question is what is the best way of passing the listofString to the StringBagAlgorithm. By doing it in the for loop for example:
_bagAlgorithm.ComputeString(MySecondListofString[i],listofString);
Or by doing it using a setter before doing the for loop. Or any other options?
Would like to know which is the best OO design for loose coupling and unit testing. Also I guess that by using a setter once and not passing the list in every call, the performance is better, but the design is worse?
It's need to be something like that :
public class StringBagAlgorithm
{
public List<string> ListofString { get; set; }
public bool ComputeString(string myString)
{
//return true or false depending on the computation with the list of strings;
return true;
}
}
public class StringsComputer
{
public List<string> FirstList { get; set; }
public List<string> SecoundList { get; set; }
public StringBagAlgorithm BagAlgorithm { get; set; }
public StringsComputer(StringBagAlgorithm bagAlgorithm, List<string> listA, List<string> listB)
{
BagAlgorithm = bagAlgorithm;
FirstList = listA;
SecoundList = listB;
}
public StringsComputer()
{
}
public void ComputeSecondList()
{
if(BagAlgorithm != null)
{
for (int i = 0; i < SecoundList.Count; i++)
BagAlgorithm.ComputeString(SecoundList[i]);
}
}
}
public class program
{
public static void Main()
{
List<string> listA = new List<string>() { "A", "B", "C", "D" };
List<string> listB = new List<string>() { "E", "F", "C", "H" };
StringBagAlgorithm sba = new StringBagAlgorithm();
StringsComputer sc = new StringsComputer() {
FirstList = listA,
SecoundList = listB,
BagAlgorithm = sba
};
sc.ComputeSecondList();
}
}
Most famous mistake to call your class ListOfStrings !!! class should be one of its type and if you want many you will do List<MyNewClass>
. learn about SRP and interfaces/abstract and try to implement them for better code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.