Is it possible to hide a static method?

I have an abstract Catalog class as follows. It has a static OpenCatalog () method, which is used to return a specific directory based on the type of location provided. After determining the type of directory, it calls the specific OpenCatalog () method for the specific type of the specific directory. For example, I may have a directory implementation that is stored in an SQL database or another that is stored in the file system. See 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, is it okay to hide the static method? I know that this is possible, but it also looks like something that needs to be done not very often. Also, is this a valid example where you can hide a static method, or is there a better way to do what I'm trying to do?

+3
3

, abstract factory . , . CatalogFactory . , (, ).

public class CatalogFactory {
  public ICatalog CreateCatalog(string location, bool openReadOnly)
  {

    if(location is filePath)
    {
        return OpenFileCatalog(string location, bool openReadOnly);
    }
    else if(location is SQL server)
    {
        return OpenSqlCatalog(string location, bool openReadOnly);
    }
    else
    {
        throw new ArgumentException("Unknown catalog type","location");
    }
  }
  FileSystemCatalog OpenFileCatalog(string location, bool openReadOnly) {
    return new FileSystemCatalog{/*init*/};
  }
  SqlCatalog OpenSqlCatalog(string location, bool openReadOnly) {
    return new SqlCatalog{/*init*/};
  }

}
+2

/ " ".

- /, .

internal static ShippingRateType ToShippingRateType(this ProviderShippingRateType rateType) { }
0

,

Catalog.OpenCatalog(...);

If you want a base class version. In fact, static methods are associated with a specific class and are not virtual. It is just a nice convenience that you can call static methods defined in the base class of the derived class.

0
source

Source: https://habr.com/ru/post/1760426/


All Articles