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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:03:49+00:00 2026-05-28T08:03:49+00:00

I am working on a game that requires resolution actions depending on the object

  • 0

I am working on a game that requires resolution actions depending on the object a ball collides with.

A lot of code within the resolution method applies to all of the different types of objects that could be hit but some is also specific to a particular object type.

The pseudo code looks like this:

resolve (Object a) {

if (some test) {
    if (Object a is rect) {
    // do one thing
    } else {
    // do something else thing
} else if (another test) {
    if (Object a is rect) {
    // do one thing
    } else {
    // do something else thing
} else if (third test) {
    if (Object a is rect) {
    // do one thing
    } else {
    // do something else thing
}
}

However, to a newbie, this feels kind of cluttered (because of the tests for object type within each if clause) but I can’t quite figure out how to improve it ? I could override the method and have behavior based on the subclass that is passed to it but then I end up with a lot the same code in both methods.

This method tests the current location of the ball (top, bottom etc) and adjusts the balls velocity accordingly. If object is a rectangle, the new velocity is different to say a circle.

Can anyone offer any assistance?

  • 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-05-28T08:03:49+00:00Added an answer on May 28, 2026 at 8:03 am

    As suggested the visitor pattern is a good idea as it supports modularisation of code. This is because the code for each test is kept its own class and method, thus helping to prevent methods becoming overly long and complex.

    It now also means you do not have to check if an Object is a Rectangle or not as this is done by the a technique known as double dispatch. Whereby the GameObject knows what instance of a class it is and therefore calls the appropriate resolve method on the Visitor.

    It also helps maintainability as if you need to treat another subclass of GameObject specifically then you first change the ResolveVisitor interface and then start changing the concrete implementations of the visitors. If you forget one by accident then compiler will not let you compile the code and point to the problem.

    Here is a verbose example of the visitor pattern:

    void resolve(GameObject obj) {
        if (first test) {
            obj.accept(new FirstTestVisitor());
        } else if (second test) {
            obj.accept(new SecondTestVistor());
        } else {
            obj.accept(new DefaultVisitor());
        }
    }
    

    interface GameObject {
        public void accept(ResolveVisitor visitor);
    }
    

    interface ResolveVisitor {
        public void resolve(Rectangle rect);
        public void resolve(GameObject obj); // default case
    }
    

    class Rectangle implements GameObject {
        public void accept(Visitor visitor) {
            visitor.resolve(this);
            // Rectangle and its subclasses will call resolve(Rectangle)
            // whereas all other GameObjects will call resolve(GameObject)
            // though other GameObjects will need their own accept method
        }
    }
    

    class FirstTestVisitor implements ResolveVisitor {
        public void accept(Rectangle rect) {
            // logic for first test and if rect goes here
        }
        public void accept(GameObject obj) {
            // logic for first test and not rect goes here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a small roguelike game, and for any object/thing that is not
I'm working on a javascript memorization game that requires that japanese characters to be
I'm working on a game that uses LWJGL and thus requires native libraries specific
I'm working on a basic iPhone game that requires a single-screen tilemap. Nothing difficult
Alright so here is my issue. I'm working a game engine that will eventually
I'm working on a game (C#) that uses a Robocode-like programming model: participants inherit
I'm working on an iPhone game that takes place on the ocean surface. Can
I am working on a web application (ASP.NET) game that would consist of a
I am working on a little card-swapping world-travel game that I sort of envision
I'm working on an isometric game for fast browsers that support <canvas> , which

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.