oop - Referring to Objects by an ID? (Java) -


i'm not java-newbie can't head around problem occured recently.

i have simulate road system in java. sake of proper oop, i've got class car , class street (and several others manage whole simulation of course^^). managed simulate jam on 1 road , have had no problem doing so.

ok, here comes question: want extend simulation 1 lonely street road system. thought of class called "roadsystem" might have array of streets , sort of connection (i thought of 'knots') allowing cars know can drive reach end of street driving on.

the problem have no idea how implement these knots. car has able ask street "hey bro, i'm @ end of you, can drive now?" , street should somehow know knot has reference , ask streets connected particular knot. how do reference? thought of id might extremely slow bigger road systems if street has search through street-ids of every knot in order find own id there. or missing obvious solution problem?

every highly appreciated!

greetings germany,

ruffy

you should have @ sourcecode of linkedlist , maybe adapt principle. road has 2 connection points, while intersection maybe 2 4?

abstract class roadelement{   //abstract simulation purpose, maybe randomized   //calculation of next direction, etc. }  class road extends roadelement{   private roadelement previous = null;   private roadelement next = null; }  class intersection extends roadelement{     private roadelement northernconnection = null;     private roadelement easternconnection = null;     private roadelement southernconnection = null;     private roadelement westernconnection = null; } 

finally, can design road-network , linking roadelements requried. during simulation don't have care concrete instaces, cause connected logically.

(you later on improve additional roadelements, such "curves" limited velocity, person-crossings stopping time etc.)

example:

   list<roadelement> roadmap = new linkedlist<roadelement>();    road r1 = new road();    intersection i1 = new intersection();    r1.setprevious(i1);    i1.setnorthernconnection(r1);    .... 

then, during simulation, can like:

car currentcar = getcurrentcar(); roadelement re = currentcar.getlocation(); if (re instanceof road){   //can drive "forward , backward?"   if ((road)re).getprevious() != null){    }    if ((road)re).getnext() != null){    } }else if (re instanceof intersection){    //check available outgoing roads    if ((intersection)re).getnorthernconnection() != null){     }    ... } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -