How to share data between separate classes in Java

后端 未结 4 953
面向向阳花
面向向阳花 2020-12-29 12:44

What is the best way to share data between separate classes in Java? I have a bunch of variables that are used by different classes in separate files in different ways. Let

相关标签:
4条回答
  • 2020-12-29 13:05

    I guess the answer to your question is the Design Pattern called Singleton. It basically allows you to get and exploits the same (and unique) instance of a class whenever you want in your system.

    This is its implementation (please forgive possible syntax errors, I did not compile it):

    class Container{
    
      //eventually provides setters and getters
      public float x;
      public float y;
      //------------
    
      private static Container instance = null;
      private void Container(){
    
      }
      public static Container getInstance(){
        if(instance==null){
           instance = new Container();
          }
          return instance;
      }
    }
    

    then if elsewhere in your code you import the Container you can write for example

    Container.getInstance().x = 3;
    temp = Container.getInstance().x;
    

    and you will affect the attributes of the unique container instance you have in your system

    In many cases it is however better to use the Dependency Injection pattern as it reduces the coupling between different components.

    0 讨论(0)
  • 2020-12-29 13:06

    I am having a hard time seeing what your problem is -- why don't you want to pass x an y as a parameter?

    Oh, well. Assuming you don't, I don't see a need for a new container class. Do it this way:

    public class SubClass1a() {  // in its own separate file
        public void doA(Top_Level_Class caller, int p, int q, int r){
           // do something that requires x,y and p, q, r
           // **to get x use the expression term caller.getX() and caller.getY()**
        }
    }
    

    Of course you need to add the public getter methods getX() and getY() to Top_Level_Class.

    If you do not want SubClass1a to be dependent on Top_Level_Class then you could create an interface that provides access to the variables.

    0 讨论(0)
  • 2020-12-29 13:08

    Blockquote The main reason I didn't want to pass x,y each time is because I have several functions in several SubClasses that each use (x,y, z, etc) and it didnt seem right to pass in like 6-8 variables each time I call a function. The sub classes used to be dependent, but I'm trying to take them out of the file so they can be used in a different project. Blockquote

    If this is the case then you are better off abstracting your variables out to a container class. If every instance of these no-longer-dependent classes is going to use a large subset of these variables then it makes sense to have them all in one instance. Variables that are logically related should be found in the same place. Your approach should be something like:

    public class Top_Level_Class(){
    Container container = new Container(x, y, p, q, r, s);
    SubClass1a a = new SubClass1a(container);
    SubClass1a b = new SubClass1b(container);
    // gets user input which changes x, y using container's getters and setters
    public void main(){
       // compute and set p, q, r, s
       a.doA();
       b.doB();
    }
    
    public class SubClass1a(Container c) {  // in its own separate file
        public void doA(c.getX, c.getY, c.getP, c.getQ, c.getR){
           // do something that requires x, y, p, q, and r
        }
    }
    
    public class SubClass1b(Container c) {  // in its own separate file
    
        public void doB(c.getX, c.getY, c.getQ, c.getR, c.getS){
           // does something else that requires x, y, q, r, and s
        }
    }
    
    public class Container(x, y, p, q, r, s) {
        //getters and setters for all variables
    }
    

    This keeps you from getting into a variable passing spaghetti-code situation, and when you want to use just one or two of these classes later your code is modular enough to port with just those classes and your container.

    0 讨论(0)
  • 2020-12-29 13:24

    This question is a bit mad - the code in the question won't compile.

    public class Main {
        public static void main(String... args) {
            MutableDataContainer m = new MutableDataContainer();
            ImmutableDataContainer i = computeImmutableData();
            new ADoer().doA(m, i);
            new BDoer().doB(m, i);
        }
        ...
    }
    
    class MutableDataContainer {
        private int x, y;
        ... // getters and setters below
    }
    
    class ImmutableDataContainer {
        private final int p, q, r, s;
        ... // getters below
    }
    

    You'll need to define ADoer and BDoer as well.

    0 讨论(0)
提交回复
热议问题