Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you implement type-specific functionality when using polymorphism does NOT make sense?
    primarykey
    data
    text
    <p>A common red flag that an OOP language is not being leveraged properly looks like this:</p> <pre><code>if (typeof(x) == T1) { DoSomethingWithT1(x); } else if (typeof(x) == T2) { DoSomethingWithT2(x); } </code></pre> <p>The standard "fix" for such design issues is to make <code>T1</code> and <code>T2</code> both share an interface, either through inheritance of a base type or implementation of a common interface (in languages that support it); for example, in C# a solution might be:</p> <pre><code>public interface IT { void DoSomething(); } </code></pre> <p>However, sometimes you want to implement functionality that differs based on the type of an object but that functionality does <em>not</em> belong within that object's type; thus polymorphism seems the wrong way to go.</p> <p>For example, consider the case of a UI that provides a view of a given clump of data. Supposing this view is capable of rendering various layouts and controls depending on the type of data being presented, how would you implement this type-specific rendering without a bunch of <code>if</code>/<code>else</code> statements?</p> <p>For reasons that I <em>hope</em> are obvious, putting the rendering logic in the type itself strikes me as a very bad decision in this case. On the other hand, without coupling the type of data object to its visual presentation I have a hard time seeing how the <code>if</code>/<code>else</code> scenario is avoided.</p> <p>Here's a concrete example: I work on a trading application which utilizes many different pricing models for various market products. These different models are represented by types inheriting from a common <code>PricingModel</code> base; and each type is associated with a completely different set of parameters. When the user wants to view the parameters for a particular pricing model (for a particular product), currently these are displayed by a form which detects the type of the model and displays an appropriate set of controls. My question is how this could be implemented more elegantly than it is currently (with a big <code>if</code>/<code>else</code> block).</p> <p>I realize this probably seems like a very basic question; it's just one of those gaps in my knowledge (of solid OOP principles? design patterns? common sense?) that I figured it's about time to fix.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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