C#C
C#3y ago
Althos

❔ Is there a better way to implement this "pattern"?

Hi, I'm trying to implement a pattern where I have a non-generic interface that exposes certain functions and can be used by any other class without the need for a generic, and then a generic version of the interface that helps restrict types properly when creating concrete implementations, here is an example
 internal interface IVisualElement
    {
        IEnumerable<IVisualElement> Children { get; }
        void Draw(IVisualElement parent);
    }

    internal interface IVisualElement<in T> : IVisualElement where T : IVisualElement
    {
        void DrawGeneric(T parent);
    }

    internal class ConcreteVisualElement : IVisualElement<Node>
    {
        public IEnumerable<IVisualElement> Children { get; } = new List<IVisualElement>();

        public void DrawGeneric(Node parent)
        {
            throw new System.NotImplementedException();
        }

        public void Draw(IVisualElement parent)
        {
            DrawGeneric(parent as Node);
        }
    }


Is there a name for what I'm trying to do, and is there a better way to implement it? Thanks a lot for your help!
Was this page helpful?