Is there a better way to display an object than using a hierarchy?

I have a class hierarchy as such:

        +-- VirtualNode
        |
INode --+                  +-- SiteNode
        |                  |
        +-- AbstractNode --+
                           |
                           +-- SiteSubNode

And the corresponding class NodeCollectionbuilt on INode. To display, NodeCollectionI need to know the final type of each member. Therefore, I need such a function

foreach (INode n in myNodeCollection)
{
    switch(n.GetType())
    {
        case(typeof(SiteNode)):
        // Display n as SiteNode
    }
}

Now this is really not an object oriented way to do this. Are there any patterns or recommended ways to do the same in your opinion?

EDIT
I was already thinking of adding a method to Displayeither Renderthe INode interface. This has a side effect associated with the presentation of the model that I really would like to avoid.

+3
source share
3 answers

, , , .

+1

:

select, , .

:

" - ( case). switch . switch, . , , . - .

switch, . , . switch . , . Extract Method Move Method, , . , /. , . "

:

  • AbstractNode - ().

  • Display() SiteNode SiteSubNode .

  • , , , AbstractNode () .

  • Display() .

  • . Display() AbstractNode INode, VirtualNode .

+1

INode - , "" .

- "" .

0

Source: https://habr.com/ru/post/1698083/


All Articles