jkteater
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
>> I had peer to strongly disagree
And what was his/hers argumentation to not do it that way? (Maybe we overlooked something)
ASKER
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.
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:
with
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.
public interface ArrayOwner {
Array getArray();
}
with
public DialogA implements ArrayOwner {
public Array getArray() {
return b; // being your array inside DialogA
}
}
andpublic class DialogB {
private arrayOwner;
public DialogB(ArrayOwner arrayOwner, etc, etc) {
this.arrayOwner = arrayOwner;
...
}
Array c = arrayOwner.getArray();
}
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
ASKER
I thought it was a OK way to do it, but I had peer to strongly disagree.