C# Design Patterns – Using Abstract Properties in Base Classes

abstract classcdesign-patternsvisual studio

I'm coding with a state pattern for an embedded device. I have a base/abstract class called State and then each discrete(concrete) state class implements the abstract State Class.

In the State Class I have several Abstract Methods. If I don't implement the abstract methods in the discrete(concrete) class, Visual Studio will give an error something like this:

…Error 1 'myConcreteState' does not implement inherited abstract member 'myAbstractState'

Now: I'm trying to create a String property for each State called StateName. Whenever I create a new concrete class, I need to define StateName. I want VS to throw an error if I don't use it. Is there a simple way to do this?

I've tried this in the abstract/base class:

public abstract string StateName { get; set; }

But I don't need to implement the Get and Set methods in each State.

Revised Question:
In an ideal situation, each State Class would be required to have StateName defines and be inherited from the abstract base class.

StateName = "MyState1"; //or whatever the state's name is

If that statement is missing then Visual Studio will generate an error as described above. Is this possible and if so, how?

Best Answer

I guess the "correct" way to do this is to have a protected constructor on the base class which requires the state name as a parameter.

public abstract class State
{
    private readonly string _name;

    protected State(string name)
    {
        if(String.IsNullOrEmpty(name))
            throw new ArgumentException("Must not be empty", "name");

        _name = name;
    }

    public string Name { get { return _name; } }
}

The concrete states then provide a public constructor which implicitly calls the base class constructor with the appropriate name.

public abstract class SomeState : State
{
    public SomeState() : base("The name of this state")
    {
        // ...
    }
}

Since the base class does not expose any other constructors (neither protected nor public) each inheriting class needs to go through this single constructor and thus needs to define a name.

Note that you don't need to provide the name when you instantiate a concrete state because its constructor takes care of that:

var someState = new SomeState(); // No need to define the name here
var name = someState.Name; // returns "The name of this state"