I have a simple class ExcelStringBuilder. Its used to build a string that can be exported to Excel. Now I need to build a CSV string also.
我有一个简单的类ExcelStringBuilder。它用于构建可以导出到Excel的字符串。现在我还需要构建一个CSV字符串。
The only difference that I see for implementing both of these classes is the delimiter that is to be used while building the string. For Excel it would be "\t" tab character and CSV its "," comma.
我看到实现这两个类的唯一区别是在构建字符串时要使用的分隔符。对于Excel,它将是“\ t”制表符,CSV为“,”逗号。
What I think is to pass the delimiter as a parameter to the ExcelStringBuilder class constructor. Will that be a right decision or should I go for factory pattern?
我认为将分隔符作为参数传递给ExcelStringBuilder类构造函数。这是一个正确的决定还是我应该选择工厂模式?
3 个解决方案
#1
3
Don't overdesign it. I would just refactor the existing class slightly:
不要过度设计它。我会稍微重构一下现有的类:
- rename it to something like
ExportStringBuilder
- 将其重命名为ExportStringBuilder
- pass the delimiter in the constructor, or as an argument of the
ToString()
function - 在构造函数中传递分隔符,或作为ToString()函数的参数
You are aware that there are some great free libraries available for this, are you? E.g. see this question
你知道有一些很棒的免费图书馆,你呢?例如。看到这个问题
#2
1
If the only difference is the delimiter, I would just pass that delimiter in. Everything else is overkill.
如果唯一的区别是分隔符,我只会通过该分隔符。其他一切都是矫枉过正的。
If there are more differences, I would create a StringBuilderFactory
that returns a IStringBuilder
. ExcelStringBuilder
and CsvStringBuilder
would both implement that interface. You would pass a parameter to the factory that tells the factory whether you want a Excel string builder or a CSV string builder and it returns the correct one.
如果存在更多差异,我将创建一个返回IStringBuilder的StringBuilderFactory。 ExcelStringBuilder和CsvStringBuilder都将实现该接口。您可以将参数传递给工厂,该工厂告诉工厂您是否需要Excel字符串构建器或CSV字符串构建器,并返回正确的参数。
#3
1
If you are planning to use a factory, you can use the template pattern along with Factory or independently. As most part of algorithm will remain same except one step and in future you may have additional steps as well (like new delimiters)
如果您打算使用工厂,可以将模板模式与Factory一起使用或单独使用。由于算法的大多数部分将保持相同,除了一步和将来您可能还有其他步骤(如新的分隔符)
Here is one approach using Template pattern. You can use "Getter" instead of GetDelimiter().
这是使用模板模式的一种方法。您可以使用“Getter”而不是GetDelimiter()。
class abstract StringBuilder
{
public virtual string GetDelimiter();
public string BuildString(string inputString)
{
// Your Code goes here...
GetDelimiter(); // Code to introduce the delimiter
// Some more of your code
}
}
class ExcelStringBuilder : StringBuilder
{
public override string GetDelimiter()
{
return "\t";
}
}
class CsvStringBuilder : StringBuilder
{
public override string GetDelimiter()
{
return ",";
}
}
#1
3
Don't overdesign it. I would just refactor the existing class slightly:
不要过度设计它。我会稍微重构一下现有的类:
- rename it to something like
ExportStringBuilder
- 将其重命名为ExportStringBuilder
- pass the delimiter in the constructor, or as an argument of the
ToString()
function - 在构造函数中传递分隔符,或作为ToString()函数的参数
You are aware that there are some great free libraries available for this, are you? E.g. see this question
你知道有一些很棒的免费图书馆,你呢?例如。看到这个问题
#2
1
If the only difference is the delimiter, I would just pass that delimiter in. Everything else is overkill.
如果唯一的区别是分隔符,我只会通过该分隔符。其他一切都是矫枉过正的。
If there are more differences, I would create a StringBuilderFactory
that returns a IStringBuilder
. ExcelStringBuilder
and CsvStringBuilder
would both implement that interface. You would pass a parameter to the factory that tells the factory whether you want a Excel string builder or a CSV string builder and it returns the correct one.
如果存在更多差异,我将创建一个返回IStringBuilder的StringBuilderFactory。 ExcelStringBuilder和CsvStringBuilder都将实现该接口。您可以将参数传递给工厂,该工厂告诉工厂您是否需要Excel字符串构建器或CSV字符串构建器,并返回正确的参数。
#3
1
If you are planning to use a factory, you can use the template pattern along with Factory or independently. As most part of algorithm will remain same except one step and in future you may have additional steps as well (like new delimiters)
如果您打算使用工厂,可以将模板模式与Factory一起使用或单独使用。由于算法的大多数部分将保持相同,除了一步和将来您可能还有其他步骤(如新的分隔符)
Here is one approach using Template pattern. You can use "Getter" instead of GetDelimiter().
这是使用模板模式的一种方法。您可以使用“Getter”而不是GetDelimiter()。
class abstract StringBuilder
{
public virtual string GetDelimiter();
public string BuildString(string inputString)
{
// Your Code goes here...
GetDelimiter(); // Code to introduce the delimiter
// Some more of your code
}
}
class ExcelStringBuilder : StringBuilder
{
public override string GetDelimiter()
{
return "\t";
}
}
class CsvStringBuilder : StringBuilder
{
public override string GetDelimiter()
{
return ",";
}
}