简体   繁体   中英

How to create a base class with static factory method in C#?

I have need to use one of two custom file readers classes; one to read a fixed width file and one for a CSV file. Each of these readers will have certain properties, etc. I want to use factory methods and have private constructors so I can run some business logic before creating the objects.

EDIT: better examples

//simple class with it's own factory method
class Class1
{
    private Class1()
    {
        //constructor code
    }
    public static Class1 CreateClass()
    {
        //do some business logic here
        return new Class1();
    }
}

What I want to be able to do is define a base class, then override the factory. I guess the problem is that a static class belongs to the base CLASS, so can never be overriden, even though they ARE inherited. This code works

public class BaseClass
{
    //some common properties / fields here
    public string SomeField;

    //some common methods here

    //empty constructor
    protected BaseClass() { }

    //cannot have a virtual static class!  
    //Would really like to make this a virtual method
    public static BaseClass CreateClass() 
    {
        throw new NotImplementedException("BaseClass is meant to be derived");
    }

    public static string DoCommonStaticThing(){
        return "I don't know why you'd ever do this"; 
    }
}

public class DerivedClass1 : BaseClass
{
    //private constructor
    private DerivedClass1() {}

    //concrete factory method
    //would really like to say "override" here
    public static BaseClass CreateClass()
    {
        DerivedClass1 d1 = new DerivedClass1();
        d1.SomeField = "I'm a derived class\r\n" + DoCommonStaticThing();
        return d1;
    }
}

EDIT: To clarify further, what I'm trying to do is put some common functionality in my base class, but define an interface for my file-format-specific methods. Some of the methods are common, but the business logic for the constructor(s) is file format specific. My code above works, but it seems to me it would be better to mark the base class factory method as virtual, and the derived class factory method as "override".

I tried to do this, but got "A static member cannot be marked as override, virtual, or abstract".

What's the right way to achieve my goals?

First, explaining your specific error message: you cannot inherit static members because they belong to the type being defined, not the instance of the type. Inheritance modifiers such as override, virtual, and abstract do not apply to static members.

Second:

Typically when you follow a factory pattern, you have a factory class whose job is to instantiate concrete classes and return those instances cast as a base class or interface. Details vary as to how the factory chooses which concrete class to instantiate, and I won't get into that, but at the fundamental level, that's what a factory does.

So in order to create a factory pattern using the example you provided, you'll need at least four types, which, following your example, could probably be named ReaderBase , ReaderFactory , CsvReader , and FixedWidthReader . Rather than ReaderBase , you might consider IReader -- the choice depends on whether your abstract class pre-implements any functionality that is shared across all Readers .

CsvReader and FixedWidthReader inherit from either IReader or ReaderBase , and ReaderFactory has at least one method called, for example, InstantiateReader , which returns an IReader or ReaderBase . InstantiateReader does the work of determining whether to instantiate a CsvReader or a FixedWidthReader , based on some external criteria.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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