?
Solved

Approach in coding...

Posted on 2005-04-05
14
Medium Priority
?
133 Views
Last Modified: 2010-05-18
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
Comment
Question by:aanandiyer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 2
14 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 13705981
How does B looks like?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13705997
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13706006
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 37

Expert Comment

by:zzynx
ID: 13706023
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
 

Author Comment

by:aanandiyer
ID: 13706085
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
 
LVL 37

Accepted Solution

by:
zzynx earned 800 total points
ID: 13706146
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 13706236
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
 

Author Comment

by:aanandiyer
ID: 13706434
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13706555
>> 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
 
LVL 92

Expert Comment

by:objects
ID: 13712742
> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 14438205
Rather clear I suppose ;°)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question