ArmoredSandwich
ArmoredSandwich

Reputation: 117

Alternative code base for achieving loop construct

I have created a rather complicated piece of (Java) code for a game I am working on. I'm fairly sure I will need to change it in the future to optimize or otherwise change the exact functioning. The code can efficiently loop through a subset of the game entities.

The problem is that while looping over this subset different codes should be executed. For example:

  • The first found game entity should be returned
  • A boolean value is to be returned when a game entity matches certain criteria (several criteria possible).

    There are several simple solutions possible:

  • Copy the code some times and in each copy change the code that is responsible for the specific needs. I prefer not to do this as future changes in the code will have to be updated at three different times. Besides that, it will make an unnecessarily mess of things. However, it does seem to be the solution that results in the most "efficient" code.
  • Check what specific need it requires using if statements. I prefer not to do this as it results in code that checks for things that are not necessary.
  • Build a collection of the game entities while looping of the subset and afterward supply this collection for methods that take care of the specific needs. I prefer not to do this as the code builds things that do not need building. I am favoring this solution.

    As one can see, both efficient and easily manageable code is required. Are there any other solutions possible with the Java programming language? I would prefer to supply the code with a pointer of some sort to the method that takes care of the specific needs but I do not know how or if that is possible in Java.

    Besides the hope for a neat solution, I ask this because I want to learn every in and out of Java! Thanks for any answers!

    Cheers, Armored

    Upvotes: 2

    Views: 150

  • Answers (3)

    ChrisH
    ChrisH

    Reputation: 4826

    Declare an interface with methods that the looping code calls, then supply the looping code with different implementations of the interface depending on what you need it do to.

    This is commonly referred to as the Template Method Pattern

    Upvotes: 3

    Daveo
    Daveo

    Reputation: 19882

    You may first want to sperate you game entities into different classes so that each class/entity contains its own code and knows how to process it self. See http://en.wikipedia.org/wiki/Factory_method_pattern

    Instead of using if/switch statements to determine what entitiy it is you could look at using Reflection. Or take it a step further and using some IoC container such as http://www.springframework.net/ this will be a step learning curve and probably overkill for now. but definatlly worth reading and learning more about.

    Upvotes: 0

    user400348
    user400348

    Reputation: 236

    I am not sure if this is a solution to your problem, but what if you had an abstract class do the basic work of looping through the entities, and then have classes extend the abstract class, and these child class would then do the more specific things. Then, runtime, you could supply to the variable of the abstract class type actual classes, based on the (user's?) specific needs.

    Upvotes: 0

    Related Questions