aybe
aybe

Reputation: 16662

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,

Upvotes: 0

Views: 55

Answers (1)

dotnetstep
dotnetstep

Reputation: 17485

Following may help.

class ScriptedImporterEditorWithPreview : ScriptedImporterEditor
{
    private EditorWithPreview editorWithPreview = null;
    public ScriptedImporterEditorWithPreview(EditorWithPreview editorWithPreview)
    {
         this.editorWithPreview = editorWithPreview;
    }
    public virtual void DrawPreview()  // based on need it is virtual or non-virtual
    {
         this.editorWithPreview.DrawPreview();
    }
}

class Cube : ScriptedImporterEditorWithPreview 
{
    
}

class Sphere : ScriptedImporterEditorWithPreview 
{
    
}

Upvotes: 1

Related Questions