Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# generics, cross referencing classes for state pattern

Tags:

c#

generics

I am trying to get into C# generics and have created a state machine with the state pattern and now I try to refactor.

I have a state, which has a reference to the object it's working on.

public abstract class AbstractState<T> where T : StatefulObject {

    protected T statefulObject;

    public AbstractState(T statefulObject) {
        this.statefulObject = statefulObject;
    }

}

and I have the object which has states, this should have a reference to its current state.

public abstract class StatefulObject<T> : MonoBehaviour where T : AbstractState<StatefulObject<T>> {

    public T state;

}

But it does not work ("the type cannot be used as type parameter 't' in the generic type or method").

What I want to achieve is something like this :

public class Monster : StatefulObject<MonsterState> {

}

public abstract class MonsterState : AbstractState<Monster> {

}

Is this possible? If it's not this way, is there another? Thx.

like image 952
ElDuderino Avatar asked Jun 13 '15 10:06

ElDuderino


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

You can abuse interfaces and variance to achieve that:

public interface IState<out TObject, in TState>
    where TObject : IStatefulObject<TObject, TState>
    where TState : IState<TObject, TState>
{
}

public interface IStatefulObject<in TObject, out TState>
    where TObject : IStatefulObject<TObject, TState>
    where TState : IState<TObject, TState>
{
}

public abstract class AbstractState<TObject> : IState<TObject, AbstractState<TObject>>
    where TObject : IStatefulObject<TObject, AbstractState<TObject>>
{
    protected TObject Object { get; private set; }

    public AbstractState(TObject obj)
    {
        Object = obj;
    }
}

public abstract class StatefulObject<TState> : IStatefulObject<StatefulObject<TState>, TState>
    where TState : IState<StatefulObject<TState>, TState>
{
    protected TState State { get; set; }
}

public class Monster : StatefulObject<MonsterState>
{
    public Monster()
    {
        State = new IdleMonsterState(this);
    }
}

public abstract class MonsterState : AbstractState<Monster>
{
    protected MonsterState(Monster monster)
        : base(monster)
    {
    }
}

public class IdleMonsterState : MonsterState
{
    public IdleMonsterState(Monster monster)
        : base(monster)
    {
    }
}

Whether it's actually a good idea is dubious, such code may be too confusing.


You could also go with the simpler (but less strongly-typed) approach:

public abstract class AbstractState
{
}

public abstract class StatefulObject
{
}

public abstract class AbstractState<TObject> : AbstractState
    where TObject : StatefulObject
{
    protected TObject Object { get; private set; }

    public AbstractState(TObject obj)
    {
        Object = obj;
    }
}

public abstract class StatefulObject<TState> : StatefulObject
    where TState : AbstractState
{
    protected TState State { get; set; }
}

public class Monster : StatefulObject<MonsterState>
{
}

public abstract class MonsterState : AbstractState<Monster>
{
    protected MonsterState(Monster monster)
        : base(monster)
    {
    }
}

This won't ensure you won't be able to assign, say a PlayerState to a Monster, you should check that at runtime.

like image 56
Lucas Trzesniewski Avatar answered Oct 15 '22 21:10

Lucas Trzesniewski