java - Tie instances of a class to another, aside from making one an inner class? -


i'm writing simulation various components, dynamic. logical structure this:

class simulation {   int getsimulationtime() {     ...   }   class thing1 {     ...   }   class thing2 {     ...   }    container <thing1> box1;   container <thing2> box2;   ...   void addthing1(thing1 thing) {      box1.add(thing);   }   thing1 getthing1() {      return box1.getone();   }   ... } 

there @ least 2 nice things structure:

  1. the thing1s , thing2s have access simulation time without having explicitly carry around simulations.

  2. the type system ensure don't accidentally mix components of 2 different simulations: if write simulation1.addthing1(simulation2.getthing1()); compiler (correctly) reject because thing1 produced not same type thing1 demanded.

the problem

the actual thing1 , thing2 class definitions large me want shove simulation class, , java doesn't allow inner class definitions pulled out separate files. there alternative way accomplish these objectives?

a partial solution

i realized can use generics second part of this, @ least externally, using inner class give each instance of simulation distinct type identity:

class simulation {    class identity {       identity(){}    }    container<thing1<identity>> box1;    thing1<identity> getthing1() {       return box1.getone();    }    void addthing1(thing1<identity> thing1) {       box1.add(thing1);    } }  class thing1<id> {    ... } 

now if attempt simulation1.addthing1(simulation2.getone()) error because relevant thing1s again have different types. solution is, however, less pretty, , doesn't take care of visibility nicely.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -