Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming" rel="nofollow noreferrer">Polymorphism:</a> </p> <p>When ever you have a select statement using the type of an object, it is a prime candidate for refactoring to polymorphism.</p> <p>Check out the book <a href="http://rads.stackoverflow.com/amzn/click/0201485672" rel="nofollow noreferrer">Refactoring</a> by Martin Fowler:</p> <p>"One of the most obvious symptoms of object-oriented code is its comparative lack of switch (or case) statements. The problem with switch statements is essentially that of duplication. Often you find the same switch statement scattered about a program in different places. If you add a new clause to the switch, you have to find all these switch, statements and change them. The objectoriented notion of polymorphism gives you an elegant way to deal with this problem.</p> <p>Most times you see a switch statement you should consider polymorphism. The issue is where the polymorphism should occur. Often the switch statement switches on a type code. You want the method or class that hosts the type code value. So use Extract Method to extract the switch statement and then Move Method to get it onto the class where the polymorphism is needed. At that point you have to decide whether to Replace Type Code with Subclasses or Replace Type Code with State/Strategy. When you have set up the inheritance structure, you can use Replace Conditional with Polymorphism."</p> <p>Here is one approach to using polymorphism in your situation:</p> <ol> <li><p>Define an abstract method in AbstractNode named something like Display().</p></li> <li><p>Then actually implement Display() in each of the SiteNode and SiteSubNode classes.</p></li> <li><p>Then, when you need to display these nodes, you could simply iterate through a collection containing items of type AbstractNode and call Display() for each. </p></li> <li><p>The call to Display() will automatically resolve to the actual concrete implementation for the real type of that item.</p></li> <li><p>Note: You could also move the Display() method from AbstractNode to the INode interface if VirtualNode is to be displayed.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload