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 9320473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:38:33+00:00 2026-06-19T03:38:33+00:00

EDIT: I really appreciate everyone’s input. I gained something from all the responses and

  • 0

EDIT: I really appreciate everyone’s input. I gained something from all the responses and learned a good deal about OOD.

I am making a simple virtual tabletop war game. To represent units on the battlefield I have the following simple class hierarchy: An abstract class Unit, and two derived classes, Troop and Vehicle.

I have another class that has a hashtable for all the units in the game. The hashtable values are of Unit type, so I can reference them in O(1) time.

For the most part, this is fine, but sometimes the caller NEEDS to know if something is a troop or a vehicle to call specific methods from those derived classes. To accommodate for this, I’ve created two get methods that will enforce the types:

  public Troop getTroop(String uniqueID) {
    Unit potentialTroop = get(uniqueID);
    if(potentialTroop instanceof Vehicle) {
      throw new InternalError();
    }
    return (Troop) potentialTroop;
  }

  public Vehicle getVehicle(String uniqueID) {
    Unit potentialVehicle = get(uniqueID);
    if(potentialVehicle instanceof Troop) {
      throw new InternalError();
    }
    return (Vehicle) potentialVehicle;
  }

(Note the class for which this belongs merely extends Hashtable, so the get method being used here is the Java’s hashtable’s get method.)

So I think this is poor OOD design because if I ever further extend unit I’m going to have to add more checks and more #get methods to this hashtable.

Am I correct in saying this? Does anyone have alternative OOD suggestions if this is the case?

  • 1 1 Answer
  • 0 Views
  • 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-06-19T03:38:34+00:00Added an answer on June 19, 2026 at 3:38 am

    Here’s a simple way to do this, not necessarily the best, but it meets these requirements:

    1. Be able to dynamically obtain a specialized type from your Unit collection
    2. Be able to add additional Unit types later on, without having to add a bunch of handler methods.

    The solution uses a ‘template’ class to perform matching:

    @SuppressWarnings("unchecked")
    public <T extends Unit> T getSpecializedUnitType(Class<T> unitTypeClass, String uniqueID) {
        Unit potentialTroop = units.get(uniqueID);
        if(potentialTroop == null) return null;
    
        return potentialTroop.getClass().equals(unitTypeClass) ?
            (T) potentialTroop : null;
    }
    

    I made the assumption that you are going to correct your code, to not extend from Map, but rather to encapsulate it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: When I say SQL Server, I'm really talking about the Management Studio. Sorry
edit I'm a fool and had a typo in my controller, it's all good
I posted another one on this,, but i had to edit it really much...
EDIT: This question was initially too general, I think. So What I really need
I installed gvim on windows vista and got really excited to edit EVERYTHING with
I am finding a lot of useful help here today, and I really appreciate
I would really appreciate it anybody could briefly explain me, what's the general approach
I am new to Android Development and I would really appreciate some advise on
Edit: This has since been solved. Thanks to everyone who helped. Invoking the method
I would really appreciate some help. I spent almost the whole morning on it.

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.