如何在Java中的不同类之间共享数据

在Java中的不同类之间共享数据的最佳方法是什么? 我有一堆变量,不同的类以不同的方式在不同的文件中使用。 让我试着说明我的问题的简化版本:

这是我之前的代码:

public class Top_Level_Class(){ int x, y; // gets user input which changes x, y; public void main(){ int p, q, r, s; // compute p, q, r, s doA(p,q,r); doB(q,r,s); } public void doA(int p, int q, int r){ // do something that requires x,y and p, q, r } public void doB(int q, int r, int s){ // does something else that requires x, y and q, r, s } } 

现在它看起来像这样:

 public class Top_Level_Class(){ int x, y; SomeClass1a a = new SomeClass1a(); SomeClass1a b = new SomeClass1b(); // gets user input which changes x, y; public void main(){ int p, q, r, s; // compute p, q, r, s a.doA(p,q,r); b.doB(q,r,s); } public class SomeClass1a() { // in its own separate file public void doA(int p, int q, int r){ // do something that requires x,y and p, q, r } } public class SomeClass1b() { // in its own separate file public void doB(int q, int r, int s){ // does something else that requires x, y and q, r, s } } 

所以无论如何,我应该每次都传递x和y(其中x,y是存储在辅助类func中的变量)?

  a.set(x,y); a.doA(p,q,r); 

我的想法是有一个特殊的容器类,其中包含x和y。 顶级类将具有容器类的实例,并使用set方法更改x,y。

 // in the top level class: Container c = new Container(x,y); a.setContainer(c); b.setContainer(c); 

我的帮助程序类也有一个容器实例,它指向与顶层相同的实例。 这样他们就可以访问与顶层相同的x,y。

我想知道我是否应该

  • 使用容器类
  • 每次将x,y加载到子类中
  • ?? 一些更好的方法?

我想你的问题的答案是名为Singleton的设计模式。 它基本上允许您在系统中随时获取和利用类的相同(和唯一)实例。

这是它的实现(请原谅可能的语法错误,我没有编译它):

 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; } } 

然后,如果您的代码中的其他地方导入了Container,您可以编写例如

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

并且您将影响系统中唯一容器实例的属性

但是,在许多情况下,使用dependency injection模式会更好,因为它会减少不同组件之间的耦合。

我很难看到你的问题是什么 – 你为什么不想把x和y作为参数传递?

那好吧。 假设你没有,我认为不需要新的容器类。 这样做:

 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()** } } 

当然,您需要将公共getter方法getX()和getY()添加到Top_Level_Class。

如果您不希望SubClass1a依赖于Top_Level_Class,那么您可以创建一个提供对变量的访问的接口。

Blockquote我不想每次传递x,y的主要原因是因为我在几个SubClasses中有几个函数,每个子函数都使用(x,y,z等)而且它似乎没有像6-8个变量一样传递每次我调用一个函数。 子类曾经是依赖的,但我试图将它们从文件中取出,以便它们可以在不同的项目中使用。 大段引用

如果是这种情况,那么最好将变量抽象出容器类。 如果这些不再依赖类的每个实例都将使用这些变量的大部分子集,那么将它们全部放在一个实例中是有意义的。 逻辑相关的变量应该在同一个地方找到。 你的方法应该是这样的:

 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 } 

这使您无法进入传递意大利面条代码情况的变量,并且当您希望稍后使用这些类中的一个或两个时,您的代码就足够模块化,只能移植那些类和容器。

这个问题有点疯狂 – 问题中的代码无法编译。

 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 } 

您还需要定义ADoerBDoer