Link to home
Start Free TrialLog in
Avatar of jkteater
jkteaterFlag for United States of America

asked on

Programming Practice - Circular Reference between Dialog Classes

Is it bad practice to make a Circular Reference  between 2 dialog classes?

Example

dialog A                                                          dialog B
 
array = b                                                        dialog B(dialog a, etc, etc) {
                                                                              parent = a;
open dailog B (dailog a, etc, etc)                    }

                                                                      array c = parent.getArrayb;


Dialog A is the parent dialog, With a button click Dialog B is opened.  I am passing the parent dialog to Dialog B.  Then I am getting the array from A.

I know that they are better ways, I just want to know how bad of practice is it to do it this way?

Looking for your input
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkteater

ASKER

Basically because I am creating a new array in dialog B by combining 2 arrays.  One from Dialog A and the other in Dialog B.  

I thought it was a OK way to do it, but I had peer to strongly disagree.
If you only need the array, you could of course also only pass that one instead of your complete DialogA.

>> I had peer to strongly disagree
And what was his/hers argumentation to not do it that way? (Maybe we overlooked something)
They claim that each Dialog should be self dependent.
You might want to consider programming to an interface, and in your implementing class, create the various Dialog objects you need there.
>> They claim that each Dialog should be self dependent.
I see. They mean, they shouldn't "know" each other; i.e. Dialog B shouldn't have any "knowledge" about the (existance of) class DialogA.
Well, then don't pass a DialogA parameter to DialogB 's constructor, but just an array.
Done. No dependencies.
Or alternatively - the comment of krakatoa inspired me ;-) - do it like this:

public interface ArrayOwner {
     Array getArray();
}

Open in new window


with
public DialogA implements ArrayOwner {

    public Array getArray() {
          return b; // being your array inside DialogA
    }

}

Open in new window

and
public class DialogB {

    private arrayOwner;

    public DialogB(ArrayOwner arrayOwner, etc, etc) {
         this.arrayOwner = arrayOwner;
         ...
    } 


    Array c = arrayOwner.getArray();
} 

Open in new window


This way the constructor of DialogB accepts an ArrayOwner as parameter.
Which can be an instance of DialogA, but also every other class instance implementing ArrayOwner. That way DialogB does not depend on DialogA, just on interface ArrayOwner.
Thanx 4 axxepting