Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4578160
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:26:46+00:00 2026-05-21T20:26:46+00:00

The problem I am trying to avoid code that looks like the following: If(object

  • 0

The problem

I am trying to avoid code that looks like the following:

If(object Is Man)
  Return Image("Man")
ElseIf(object Is Woman)
  Return Image("Woman")
Else
  Return Image("Unknown Object")

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:

Code structure:

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 Implementation (key classes):

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 < 18)
            return new Image("Image of Boy");
        else
            return new Image("Image of Man");
    }

    public static Image GetImageForObject(Woman woman)
    {
        if (woman.Age < 70)
            return new Image("Image of Woman");
        else
            return new Image("Image of Granny");
    }
}

My scenario:
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).

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.

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).

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:26:47+00:00Added an answer on May 21, 2026 at 8:26 pm

    Use a factory:

    public class ImageFactory
    {
        Dictionary<Type, Func<IPerson, Image>> _creators;
    
        void Assign<TPerson>(Func<IPerson, Image> imageCreator) where T : IPerson
        {
           _creators.Add(typeof(TPerson), imageCreator);
        }
    
       void Create(Person person)
       {
           Func<IPerson, Image> creator;
           if (!_creators.TryGetValue(person.GetType(), out creator))
              return null;
    
           return creator(person);
       }
    }
    

    Assign factory methods:

    imageFactory.Assign<Man>(person => new Image("Man");
    imageFactory.Assign<Woman>(person => new Image("Big bad mommy");
    imageFactory.Assign<Mice>(person => new Image("Tiny little mouse");
    

    And use it:

    var imageOfSomeone = imageFactory.Create(man);
    var imageOfSomeone2 = imageFactory.Create(woman);
    var imageOfSomeone3 = imageFactory.Create(mice);
    

    To be able to return different images for men you can use a condition:

    factory.Assign<Man>(person => person.Age > 10 ? new Image("Man") : new Image("Boy"));
    

    For clarity you can add all more complex methods to a class:

    public static class PersonImageBuilders
    {
        public static Image CreateMen(IPerson person)
        {
            if (person.Age > 60)
                return new Image("Old and gready!");
            else
                return new Image("Young and foolish!");
    
        }
    }
    

    And assign the method

    imageFactory.Assign<Man>(PersonImageBuilders.CreateMen);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON object that looks like this. [ { id : 23,
I've ran into a problem while trying to test following IRepository based on NHibernate:
I'm trying to solve the 3n+1 problem and I have a for loop that
Having a problem trying to create a function, as part of a BizTalk helper
I'm running into a problem trying to anchor a textbox to a form on
(see here for the problem I'm trying to solve) How do you get hibernate
I have a problem I'm trying to solve involving interfaceing a C++ program with
I have a small math problem I am trying to solve Given a number
In trying to figure out this problem (which is still unsolved and I still
In trying to solve the ajax back button problem I have found the Really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.