• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

Approach in coding...

Hi,

Im coding an application in java, i have two classes namely A & B
class A contains some 5 to 6 datastructures(like hashtable, vector etc.) which needs to be shared between classes A &B.

class A looks like this
class A
{
   Hashtable t; // needs to be shared with class B
   Vector v;   //  needs to be shared with class B
    public A()
    {
        //which is the most suggested way.
        B( this );
        //or
        B( t, v);
    }

    public getT(){ return t; }
    public getV(){ return v; }
}

0
aanandiyer
Asked:
aanandiyer
  • 8
  • 2
1 Solution
 
zzynxSoftware engineerCommented:
How does B looks like?
0
 
zzynxSoftware engineerCommented:
I would pass an A instance to the constructor of B:

public class B

     private A aInstance;

     public B(A aInstance) {
         this.aInstance = aInstance;
     }

}
0
 
zzynxSoftware engineerCommented:
Once you have the A instance you can call whatever you need getT() or getV()
If in the future you'll have to add another data member

      Vector v2;   //  needs to be shared with class B

      public Vector get2ndVector() { return v2; }

you don't need to change anything to the B constructor.
Since you have the A instance available, you can call get2ndVector() on it giving you the new data member
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
zzynxSoftware engineerCommented:
But I don't think you'll have this:

>>    public A() {
>>        B( this );
>>        //or
>>        B( t, v);
>>    }

Why would you create a B instance in the A constructor.
I think you'll have more something like

A myVarA = new A();
B myVarB = new B(myVarA);
0
 
aanandiyerAuthor Commented:
thanks for ur response zzynx.

Class A is the main class which instantiates other classes. but why do u recommend passing the instance of class A to class B, can you please explain.

I have one more question, if we pass the instance of class A, wont there be a possibility of cyclic dependency??
0
 
zzynxSoftware engineerCommented:
>> if we pass the instance of class A, wont there be a possibility of cyclic dependency??
No. As long as you don't (need to) pass an instance of class B to A

>> why do u recommend passing the instance of class A to class B, can you please explain
I thought I did.
Once your other classes have the A instance they can call whatever they need on that base instance

>> Class A is the main class which instantiates other classes.
So then you'll have something like

public class A
{
   private Hashtable t; // needs to be shared with class B
   private Vector v;   //  needs to be shared with class B
   private B b;  

   public A() {
        b = new B(this);
   }

   public Hashtable getT(){ return t; }
   public Vector getV(){ return v; }
}



0
 
zzynxSoftware engineerCommented:
Then in B you can have

public class B

     private A aInstance;

     public B(A aInstance) {
         this.aInstance = aInstance;
     }

     public void addToVector(String item) {      // <<<<<<<<<
        if (aInstance!=null)
           aInstance.getVector().add(item);
     }
}
0
 
aanandiyerAuthor Commented:
Yep i understand your point. But assume therez a class Car which has a contained object Tyre

Class Car
{
     Tyre t;
     Bolt b;
     Car()
     {
          t = new Tyre();
      }
}

if we provide the Car instance instead of the Bolt instance to the Tyre class, wont it give the entire control of the Car to Tyre class ?? is that behaviour right??
0
 
zzynxSoftware engineerCommented:
>> won't it give the entire control of the Car to Tyre class ??
Affirmative. TheTyre class then has access to all functions of the Car class.
*IF* you don't like that, it's indeed better to pass the Car's details members, like the Bolt instance.
That's the tradeoff.
0
 
objectsCommented:
> wont it give the entire control of the Car to Tyre class ??

correct

> is that behaviour right??

no it is not, the tyre does not need to know anything about the car it is connected to.

0
 
zzynxSoftware engineerCommented:
Rather clear I suppose ;°)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now