samedi 3 juillet 2021

Design pattern to inherit derived members from non-modifiable base classes?

I have the following chicken/egg inheritance problem:

Here, base classes I can derive from, but they're on a framework, thus, I can't modify them:

class Editor
{

}

class ScriptedImporterEditor : Editor
{
}

Here, are classes in my project:

An editor with a preview, this works as expected, Cylinder and Torus have DrawPreview:

class EditorWithPreview : Editor
{
    public void DrawPreview(){}
}
   
class Cylinder : EditorWithPreview
{
    // DrawPreview is available
}

class Torus : EditorWithPreview
{
    // DrawPreview is available
}

But now I need a scripted importer editor that can also preview:

class ScriptedImporterEditorWithPreview : ScriptedImporterEditor
{
    // cannot inherit EditorWithPreview as it's not a ScriptedImporterEditor
}

class Cube : ScriptedImporterEditorWithPreview 
{
    // unable to use DrawPreview
}

class Sphere : ScriptedImporterEditorWithPreview 
{
    // unable to use DrawPreview
}

So basically,

  • I can't change neither Editor nor ScriptedImporterEditor as I don't own them
  • I therefore cannot import the logic of EditorWithPreview to ScriptedImporterEditor
  • Cube and Sphere can't inherit and use DrawPreview

Aucun commentaire:

Enregistrer un commentaire