1

I have a situation where my 'agent' objects contain a vast amount of data, including graphics, physics, and now also ai.

I was previously coding components of these 'agents' in separate objects, that were parallel. Now I realize, since the agents are contained in a re-sizable ArrayList, that if one of the agents is destroyed, the indices will no longer be parallel to the ai components.

Truth be told, the agent class is already 10 pages long, and it is very sensible to contain the ai methods and data in a separate object. One issue with this, of course, is that the methods will be 'reproduced' in a way, because instead of having one ai object that will accept and process the data from the many agents, I need one ai object per every agent object.

I asked about this once before and was told that having multiple instances of methods has no effect on performance or memory bloat, so that shouldn't be an issue.

I am not sure how I can solve this problem, except by possibly storing an agent_id in the agent object as well as the ai object and then running a search on each list whenever I need to call them. Needless to say, that is terrible way of doing things performance wise.

In C++, the simple solution to this problem would be a pointer, where the pointer to the proper ai instance would be contained in the agent data. I don't know how to do this, so my best solution is to just cram more data into the agent object and have it passed as an argument to the ai object.

Is there any better way to solve this 'parallel mismatching' problem.

Edit>>>
I know I can stuff all of the data into the agent class. What I was taught, is having a 30 page class is an example of bad oop structure.

My question is, how can I create a /reference/ to store in the agent class, while keeping all of the ai data encapsulated in the ai module.

Edit>> Example

    public class Agent{

    //pseudo code that represents what I need (yes the class declaration order is wrong)
    AI_ref   = new Reference(ai_module);
    Graphics_ref = new Reference(graphics_module);

    int[][] physics_data; //Like I said, 10 pages of this

    void method1(){}
    void method2(){}
    //....

    }

    public class graphics_module{

    int[][] graphics_data; //this is also about 10 pages

    void method1(){}
    void method2(){}
    //....

    }

    public class ai_module{

    int[][] ai_data; //this will likely span 5ish pages

    void method1(){}
    void method2(){}
    //....

    }

}
bigcodeszzer
  • 916
  • 1
  • 8
  • 27
  • 2
    One big concept of OOP is that all of an object's state/data is encapsulated within the object (as opposed to spread around in external data structures, such as your arraylists). That way, it gets much easier to manage them as a unit. So I would say your plan to have the Agent contain all the data it needs sounds like a good idea. – Thilo Mar 02 '16 at 01:33
  • @Thilo One other aspect of OOP is modularity. I have no problem with keeping references to all of the data in the agent object, but having a 30 page class is definitely /not/ good oop form. I need pointers, and java doesn't support them. – bigcodeszzer Mar 02 '16 at 01:36
  • Objects can be composed of other objects. That avoids 30 page classes. Objects in Java are shared using references. This is probably what you mean by "pointers". – Thilo Mar 02 '16 at 01:56
  • @thila yah, basically. How do you do that? – bigcodeszzer Mar 02 '16 at 01:57
  • @Thilo Note, I also don't want to lose the encapsulation. I prefer when all of the data and methods of one module are self contained and only the minimum necessary input is passed in, and likewise returned out. – bigcodeszzer Mar 02 '16 at 01:59
  • Why are you storing application data inside source code? Can't you store it in external resources (files, databases, redis, hadoop...) and look it up when you need it? – Paul Hicks Mar 02 '16 at 01:59
  • `int[][] physics_data; //Like I said, 10 pages of this ` Are you saying you have 10 pages of instance variable declarations? Like 500 instance variables? There must be a way to group these together into smaller functional units. – Thilo Mar 02 '16 at 01:59
  • @Thilo In jbox2d, a complex physical structure is made up of many, many bodydef, fixtures and joints. – bigcodeszzer Mar 02 '16 at 02:01
  • @Thilo In the case of graphics, using opengl requires a similar layout to create images, shapes, colors, rendering etc. As for the ai, I'm doing something pretty complicated that is in the realm of a genetic algorithm, so it will be long, but may be contained in multiple objects. – bigcodeszzer Mar 02 '16 at 02:02

2 Answers2

4

Parallel arrays are a 1960's construct developed when Basic and Fortran were modern languages. These languages had no other data structures.

Since the 1990s with OO development, if you have several different types of data that belong together, you create an object to hold references to those bits of data such that you don't need to worry about parallel anything.

I strongly suggest you refactor your code to modern best practices and use objects. Since you've provided no explicit details, this is about the most explicit answer that can be given.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • One other aspect of OOP is modularity. I have no problem with keeping references to all of the data in the agent object, but having a 30 page class is definitely /not/ good oop form. I need pointers, and java doesn't support them. – bigcodeszzer Mar 02 '16 at 01:37
  • 2
    You _think_ you need pointers, and you _think_ Java doesn't support them. Both these statements are actually not true. They're called _references_ in Java, and a Java class can contain references to objects of other classes. And also, you don't need to write a 30 page class if you do it correctly. – Jim Garrison Mar 02 '16 at 01:39
  • Which is what my question is... how do you do that? – bigcodeszzer Mar 02 '16 at 01:40
  • Show us a limited-scope example of your current code (edit your post) and we can show you how to eliminate the parallel arrays. – Jim Garrison Mar 02 '16 at 01:41
1

To create your references, try

public class Agent{

//pseudo code that represents what I need (yes the class declaration order is wrong)
    ai_module AI_ref   = new ai_module();
    graphics_module Graphics_ref = new graphics_module();

    int[][] physics_data; //Like I said, 10 pages of this

    void method1(){}
    void method2(){}
    //....

}

As for your parallel arrays, your example doesn't really provide enough detail to demonstrate what you are trying to do.

//pseudo code that represents what I need (yes the class declaration order is wrong)

Do you mean defining Agent before defining ai_module and graphics_module is wrong? It's not. The java compiler will have no issue with that.

You should probably work through a basic java tutorial. I think it will address many of your issues.

bradimus
  • 2,472
  • 1
  • 16
  • 23