我刚刚编写了一个类,并意识到它的所有公共功能都封装在一个方法中.它没有属性,没有共享资源,并且不需要构造函数重载并处理它使用的任何内容.它看起来像这样:
public class ReportGenerator
{
public string GenerateReport(List<SomeClass> stuffToReportOn)
{
string fileName = String.Empty;
using(var reportDs = CreateDataSet(stuffToReportOn))
{
//do the stuff with the third party tool that
//creates the report.
//Construct the filename.
//Save the report.
}
return fileName;
}
private TypedDataSetRequiredByThirdPartyLib CreateDataSet(List<SomeClass> reportItems)
{
//create the dataset, calling two other private methods
//to build the tables/rows
}
}
Run Code Online (Sandbox Code Playgroud)
在我完成重构之后,我意识到这个类可能完全是静态的.我的问题是,应该吗?是否应将在一个公共方法中封装其所有功能的类设为静态?
不,有什么预期的好处?
更重要的是潜在的错误.如果在你的类中声明一个静态变量,它只会被初始化一次,它的值将持续存在,并且每个调用的行为可能会有所不同.容易过度查看,这可能很难调试.
归档时间: |
|
查看次数: |
191 次 |
最近记录: |