Home arrow static arrow Java Programming [Archive] - An OOP model idea required
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - An OOP model idea required
This topic has 7 replies on 1 page.

Posts:41
Registered: 6/13/04
An OOP model idea required  
Jun 25, 2004 10:32 PM



 
hi all,

I have a requirement.

There will be two independent classes. A and B.
There will be two instances oA and oB.

I have to link an instance of oA as its created to an instance of oB.
I did this by creating instance oB in Class A's constructor..

But theres another requirement, that some methods in class B will
need to use values of variables in class A.
meaning,

an instance of A will need an instance of B for the corresponding instance
of A,
and the instance of B will use A's instance variables, or viceversa.

public oB.something(oA){

oB.n1= oA.t1 + oA.t2
----
----
}

I have currently done the above..
Is it ok. is it a cyclic ref.

whats your suggestion?

Thanks
'Harish
 

Posts:49
Registered: 6/17/04
Re: An OOP model idea required  
Jun 26, 2004 12:50 AM (reply 1 of 7)



 
You have quite a jumbled up problem man Whew!!!!!! have to think about it after I understand it :D
 

Posts:453
Registered: 11/6/04
Re: An OOP model idea required  
Jun 26, 2004 1:08 AM (reply 2 of 7)



 

You could use inner classes. In your case you could make class B an inner class of class A
but your statement "There will be two independent classes. A and B. " will not be right
 

Posts:453
Registered: 11/6/04
Re: An OOP model idea required  
Jun 26, 2004 1:16 AM (reply 3 of 7)



 
would something of this sort help you?

public class Outer{   int i=0;  Inner objInner;    public void setInner(Inner obj)  {   objInner = obj;  }        class Inner  {         public void printI()     {            System.out.println(i);//can reference outer  vars           }    }        public static void main(String arg[])  {    Outer objOuter = new Outer();    Outer.Inner objInner = objOuter.new Inner();    objOuter.setInner(objInner);    }   } 
 

Posts:11,186
Registered: 06.04.04
Re: An OOP model idea required  
Jun 26, 2004 4:25 AM (reply 4 of 7)



 
So those two classes A and B aren't so independent after all, i.e. objects of both classes need to be
paired (or linked) to each other. How about this --
public class A {   private B that;   public A() { this(null); }   public A(B b) { link(b); }   public void link(B that) {      if (this.that != that) {         this.that= that;         if (that != null) that.link(this);      }   }}public class B {   private A that;   public B() { this(null); }   public B(A a) { link(a); }   public void link(A that) {      if (this.that != that) {         this.that= that;         if (that != null) that.link(this);      }   }} 

kind regards,

Jos

 

Posts:41
Registered: 6/13/04
Re: An OOP model idea required  
Jun 26, 2004 7:25 AM (reply 5 of 7)



 

In reply to jSweep's suggestion,

<div class="jive-quote"> public static void main(String arg[]){Outer objOuter = new Outer();Outer.Inner objInner = objOuter.new Inner();objOuter.setInner(objInner); } 

Arent we creating a new instance of the inner class. Does that mean that a new innerClass instance will be created everytime.. whereas i want it such that a particualr instance of outerObj always refers to a particular instance of the innerObj.

i believe putting
Outer.Inner objInner = objOuter.new Inner();

in the constructor of the
outerObj is a solution..

right ?
 

Posts:453
Registered: 11/6/04
Re: An OOP model idea required  
Jun 27, 2004 10:29 PM (reply 6 of 7)



 
Yes..If you don't want to create an innerobj each time, you could put it in outer constructor.
something like

 public Outer() {  objInner = new Inner();  }


and remove the setInner method.
 

Posts:453
Registered: 11/6/04
Re: An OOP model idea required  
Jun 27, 2004 10:37 PM (reply 7 of 7)



 
Yes..If you don't want to create an innerobj each time, you could put it in outer constructor.
something like

 public Outer() {  objInner = new Inner();  }


and remove the setInner method.
 
This topic has 7 replies on 1 page.