简体   繁体   English

OOP设计设置器与方法中的传递参数

[英]OOP Design setter vs passing parameter in method

I have a doubt about good OOP design in this case: 在这种情况下,我对良好的OOP设计有疑问:

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). 想象一下,我有一个存储字符串列表的类,并且有一个名为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. 我返回的是true还是false取决于我对该字符串“相关”到其中一个字符串的计算是否为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: 字符串列表存储在另一个名为ListOfStrings的类中,该类具有对StringBagAlgorithm的引用,因此:

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. 我的问题是将listofString传递给StringBagAlgorithm的最佳方法是什么。 By doing it in the for loop for example: 例如,通过在for循环中执行此操作:

_bagAlgorithm.ComputeString(MySecondListofString[i],listofString); _bagAlgorithm.ComputeString(MySecondListofString [I],listofString);

Or by doing it using a setter before doing the for loop. 或者在执行for循环之前使用setter进行操作。 Or any other options? 还是其他选择?

Would like to know which is the best OO design for loose coupling and unit testing. 想知道哪种是用于松耦合和单元测试的最佳OO设计。 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? 另外我猜想,通过一次使用setter而不在每个调用中传递列表,性能会更好,但设计会更糟吗?

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 !!! 调用类ListOfStrings的最著名错误! class should be one of its type and if you want many you will do List<MyNewClass> . class应该是它的类型之一,如果需要很多,可以使用List<MyNewClass> learn about SRP and interfaces/abstract and try to implement them for better code. 了解有关SRP和接口/抽象的信息,并尝试实现它们以获得更好的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM