Note that there are some explanatory texts on larger screens.

plurals
  1. POOverloading with a class hierarchy - most derived not used
    text
    copied!<p><strong>The problem</strong></p> <p>I am trying to avoid code that looks like the following:</p> <pre><code>If(object Is Man) Return Image("Man") ElseIf(object Is Woman) Return Image("Woman") Else Return Image("Unknown Object") </code></pre> <p>I thought I could do this through method overloading, but it always picks the least derived type, I assume this is because the overloading is determined at compile time (unlike overriding), and therefore only the base class can be assumed in the following code:</p> <p><strong>Code structure:</strong></p> <pre><code>NS:Real RealWorld (Contains a collection of all the RealObjects) RealObject Person Man Woman NS:Virtual VirtualWorld (Holds a reference to the RealWorld, and is responsible for rendering) Image (The actual representation of the RealWorldObject, could also be a mesh..) ArtManager (Decides how an object is to be represented) </code></pre> <p><strong>Code Implementation (key classes):</strong></p> <pre><code>class VirtualWorld { private RealWorld _world; public VirtualWorld(RealWorld world) { _world = world; } public void Render() { foreach (RealObject o in _world.Objects) { Image img = ArtManager.GetImageForObject(o); img.Render(); } } } static class ArtManager { public static Image GetImageForObject(RealObject obj)// This is always used { Image img = new Image("Unknown object"); return img; } public static Image GetImageForObject(Man man) { if(man.Age &lt; 18) return new Image("Image of Boy"); else return new Image("Image of Man"); } public static Image GetImageForObject(Woman woman) { if (woman.Age &lt; 70) return new Image("Image of Woman"); else return new Image("Image of Granny"); } } </code></pre> <p><strong>My scenario:</strong> Essentially I am creating a game, and want to decouple real-world classes (such as a man), from on-screen classes (an image of a person). The real world object should have no knowledge of it's on-screen representation, the representation will need to be aware of the real object (to know how old the man is, and therefore how many wrinkles are drawn). I want to have the fallback where if a RealObject is of an unknown type, it still displays something (like a big red cross).</p> <p>Please note that this code is not what i'm using, it's a simplified version to keep the question clear. I may need to add details later if applicable, I'm hoping the solution to this code will also work in the application.</p> <p>What's the most elegant way to solve this? - Without the RealObject itself holding information on how it should be represented. The XNA game is a proof of concept which is very AI heavy, and if it proves doable, will be changed from 2D to 3D (probably supporting both for lower end computers).</p>
 

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