隐藏静态方法可以吗?
我有一个抽象 Catalog 类,如下所示。它有一个静态方法 OpenCatalog(),用于根据提供的位置类型返回特定的具体目录。一旦确定了目录类型,它就会调用正确的具体目录类型的特定 OpenCatalog() 方法。例如,我可能有一个存储在 SQL 数据库中的 Catalog 实现,或者另一个存储在文件系统中的 Catalog 实现。请参阅下面的代码。
public abstract class Catalog
{
public static ICatalog OpenCatalog(string location, bool openReadOnly)
{
if(location is filePath)
{
return FileSystemCatalog.OpenCatalog(string location, bool openReadOnly);
}
else if(location is SQL server)
{
return SqlCatalog.OpenCatalog(string location, bool openReadOnly);
}
else
{
throw new ArgumentException("Unknown catalog type","location");
}
}
...
}
public abstract class FileSystemCatalog:Catalog
{
public static new ICatalog OpenCatalog(string location, bool openReadOnly)
{
//Deserializes and returns a catalog from the file system at the specified location
}
...
}
public abstract class SqlCatalog:Catalog
{
public static new ICatalog OpenCatalog(string location, bool openReadOnly)
{
//creates an returns an instances of a SqlCatalog linked to a database
//at the provided location
}
...
}
首先,隐藏静态方法可以吗?我知道这是可能的,但这似乎也是人们不应该经常做的事情。这也是一个有效的例子,可以隐藏静态方法,还是有更好的方法来完成我想做的事情?
I have an abstract Catalog class as follows. It has a static method OpenCatalog() which is used to return a specific concrete catalog based on the type of location provided. Once it has determined the type of catalog it then calls a specific OpenCatalog() method of the correct concrete catalog type. For example I may have an implementation of Catalog that is stored in a SQL database, or another which is stored in a file system. See the code below.
public abstract class Catalog
{
public static ICatalog OpenCatalog(string location, bool openReadOnly)
{
if(location is filePath)
{
return FileSystemCatalog.OpenCatalog(string location, bool openReadOnly);
}
else if(location is SQL server)
{
return SqlCatalog.OpenCatalog(string location, bool openReadOnly);
}
else
{
throw new ArgumentException("Unknown catalog type","location");
}
}
...
}
public abstract class FileSystemCatalog:Catalog
{
public static new ICatalog OpenCatalog(string location, bool openReadOnly)
{
//Deserializes and returns a catalog from the file system at the specified location
}
...
}
public abstract class SqlCatalog:Catalog
{
public static new ICatalog OpenCatalog(string location, bool openReadOnly)
{
//creates an returns an instances of a SqlCatalog linked to a database
//at the provided location
}
...
}
First in general is it ok to hide a static method? I know it's possible to do, but it also just seems like something that one shouldn't do very often. Also is this a valid example where it's ok to hide a static method, or is there a better way to do what I'm trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您正试图以一种非常尴尬的方式创建一个抽象工厂。实际发生的情况是您违反了单一职责原则并将目录创建问题与目录问题混合在一起。您需要做的就是使
CatalogFactory
成为非静态类。这使您可以灵活地进行以后的任何操作(例如依赖注入)。It looks like you are trying to create an abstract factory in a very awkward manner. What actually happens is you are violationg Single Responsibility Principle and mixing the catalog creation concern with the catalog concern. What you need to do is to make
CatalogFactory
non-static class. This gives you the flexibility to whatever you please later on (eg Dependency Injection).您并没有真正隐藏它,因为
如果您想要基类版本,您总是可以这样做。事实上,静态方法与特定的类相关联,并且不是虚拟的。您可以在派生类上调用基类中定义的静态方法,这非常方便。
You aren't really hiding it because you can always do
If you want the base class version. In fact, static methods are associated with a specific class and aren't virtual. It's just a nice convenience that you can call static methods defined in a base class on the derived class.
我从未在“代码气味”方面找到反对使用私有/内部静态方法的论点。
一个很好的实际示例可能是某种服务/实用程序库中的扩展方法,您只想在该库中扩展。
I have never found an argument against the use of private/internal static methods in terms of "code smell".
A good practical example might be an extension method inside some sort of service/utility library that you want to only extend within that library.