Solved

Programming Practice - Circular Reference between Dialog Classes

Posted on 2013-01-17
8
386 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
 

Author Comment

by:jkteater
ID: 38787749
They claim that each Dialog should be self dependent.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 82
Updating Java 9 87
network + 7 73
backtracking recursion  code 19 40
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now