0

I'm new to OO programming and having troubles with "global/shared" data. The problem is that I fetch data (about 60 variables) from a Data-BUS every 20ms. Then I have a lot of classes which needs this data and also modify it.

My solution was creating a class including all the variables so all "global" data is in one object. And then every class has a reference to this object in the constructor. It works but I'm sure thats not a good way how to do it. How would you solve this problem? I would be really grateful for any advice!!

Here is an Java example I know there should be getter/setter:

public class MainProgram {

    private GlobalData iGlobalData;
    private TestClass iTestClass;

    public MainProgram(){
    iGlobalData = new GlobalData(this);
    iTestClass =new TestClass(this);
    }


    public static void main(String[] args) {


    MainProgram h =new MainProgram();

    iTestClass.docalc();
    System.out.println(iGlobalData.test);
}


}

public abstract class MainProgramComponent {

    private MainProgram iMainProgram;

}


public class GlobalData extends MainProgramComponent{

    public int test;


    GlobalData(MainProgram iMainProgram){
    super(iMainProgram);
    }


}

public class TestClass extends MainProgramComponent{

    private GlobalData iGlobalData;

    TestClass(MainProgram iMainProgram){
    super(iMainProgram);
    this.iGlobalData=iMainProgram.getiGlobalData();
    }

    public void docalc(){

    iGlobalData.test =iGlobalData.test+1;

    }
}
  • So you read the data from the bus in to a single instance of this object that is shared between many objects? Are there many threads involved as well? – DaveH Jun 26 '13 at 10:03
  • If more than one thread can access these variables you must make sure that the getter and setter are synchronized (i.e. no concurrent modification can occur). [to be exact, not all getter needs to be synchronized. For more details have a look at parallel programming] – Burkhard Jun 26 '13 at 10:04
  • 1. Does the data need to be persisted for "offline" processing or is it discarded once read? Are 20ms enough to process the data? 2. "GlobalData" is not a good name. Pick a name that describes the purpose or meaning of the data. – Joni Jun 26 '13 at 10:09
  • @DaveHowes: Yes i read the data from the bus in to a single instance of this object that is shared between many objects and there are more threads involved. – user2523419 Jun 26 '13 at 10:12
  • OK - Life will become more difficult then. Do you have one thread reading the bus every 20 ms, and a load of other threads waiting for the update to happen? And what about modifications to the shared data? What happens to them? – DaveH Jun 26 '13 at 10:15
  • @Joni The data need to be pesistend and is not discarded once read. But every 20ms the data on the bus changes. So basically there are two programs and they communicate via the bus. Both programs run in an own thread. I have a class called InputManager which save the data from the bus on the class Blackboard that's "GlobalData". – user2523419 Jun 26 '13 at 10:26
  • But i don't have to give the other program an input every 20ms. I hope thats not too confusing – user2523419 Jun 26 '13 at 10:28
  • @DaveHowes sorry I got your question wrong. Now my program has only one thread. Basacally I read the bus data then check if there are any changes from the last state. if yes then decide if i should give the other programm an input. if yes write it on the bus. if no start from the beginning – user2523419 Jun 26 '13 at 10:37

0 Answers0