2

I was wondering (there has to be an easy solution) if there is a way to make a number of objects, and use them in other classes and objects from those classes, without having to update them (in Java). An example:

Class A, B and C make objects a b and c. Then I have class D, which makes object d. Now I want to use d in a, change it, use d in b, change it there and use d in c and still have all the changes from a and b. I know it's possible with using d as an argument in the functions, and returning d, but that is not useful in the program I'm making at the moment.

Any help?

Ok, My example in code:

public class A {
   D d = new D ();
   A(D dObject){
     this.d = dObject;
   }
   public void add () {
     DObject.add(5);
   }
}

public class B {
   D d = new D ();
   B(D dObject){
     this.d = dObject;
   }
   public void add () {
     DObject.add(5);
   }
}

public class C {
   D d = new D ();
   C(D dObject){
     this.d = dObject;
   }
   public void add () {
     DObject.add(5);
   }
}
public class D {
   public int x=0;
   public void add (int y){
      x +=y;
   }
}

// in main class:
D d = new D();
A a = new A(d);
B b = new B(d);
C c = new C(d);

a.add();
b.add();
c.add();

// d.x should give 15 now. 
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Hidde
  • 11,493
  • 8
  • 43
  • 68
  • 3
    D makes d??.. You need to rephrase your question better. – uncaught_exceptions Apr 19 '11 at 21:06
  • This is the most complicated question I think I've ever read. Please try to rephrase this into something that makes sense...toooo many things to keep track of.... – Chris Thompson Apr 19 '11 at 21:08
  • Also, that would require 3 different instances of "d" or some sort of versioning engine... – Chris Thompson Apr 19 '11 at 21:10
  • Sound like you're looking for a global variable. These tend to be a bad idea – barrowc Apr 19 '11 at 21:18
  • This sounds like a very BAD idea, and goes against just about anything anyone has ever written about object oriented programming. Why can't you pass it as an argument? What do these objects / classes do? Why can't they be static methods? – cwharris Apr 19 '11 at 21:27
  • I updated, see what you think now. I do NOT want to update, that's what I want to stop doing. – Hidde Apr 19 '11 at 21:33

3 Answers3

3

Take a look at the Singleton pattern It only works if you want a single instance of class D, but you can create a singleton to store references of other objects. Hope this helps.

Dave
  • 532
  • 5
  • 8
1

I'm not sure if I understand right - the code you posted does exactly what you say (d.x is 15), if you correct the syntax errors.

Here is a version that should work:

public class A {
   D d;
   A(D dObject){
     this.d = dObject;
   }
   public void add () {
     d.add(5);
   }
}

public class B {
   D d;
   B(D dObject){
     this.d = dObject;
   }
   public void add () {
     d.add(5);
   }
}

public class C {
   D d;
   C(D dObject){
     this.d = dObject;
   }
   public void add () {
     d.add(5);
   }
}
public class D {
   public int x=0;
   public void add (int y){
      x +=y;
   }
}

// in main class:
D d = new D();
A a = new A(d);
B b = new B(d);
C c = new C(d);

a.add();
b.add();
c.add();

// d.x should give 15 now. 

If you want d to stay the same instead of changing (e.g. d.x == 0 at the end), you can do one of these:

  • make clones of the parameter dObject in the constructor of A, B, C, so each has its own D object.
  • make D immutable and let its add method instead return a new D object. The add method of A, B, C would then have the implementation d = d.add(5);.
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
-1

I think you mean Immutable Objects based on your description.

lobster1234
  • 7,679
  • 26
  • 30