Solved

Programming Practice - Circular Reference between Dialog Classes

Posted on 2013-01-17
8
394 Views
Last Modified: 2013-01-17
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
0
Comment
Question by:jkteater
  • 5
  • 2
8 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 38787552
I had my doubts when I read "circular reference".
But what you meant isn't really "circular reference"

If - in dialog B - you need to access the array of dialog A, imo, there's nothing wrong with what you do.

But one could have questions about *why* you need to access that array in Dialog B.
Is it really needed? Or could a better design eleminate that need?
0
 

Author Comment

by:jkteater
ID: 38787593
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38787654
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)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:jkteater
ID: 38787749
They claim that each Dialog should be self dependent.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38787768
You might want to consider programming to an interface, and in your implementing class, create the various Dialog objects you need there.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38787844
>> 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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38787886
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38787892
Thanx 4 axxepting
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 64
tomcat administrtor 12 61
hibernate example using maven 12 59
running on tomcat not jboss eap 7.0 3 19
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 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