aspose file tools*
The moose likes Beginning Java and the fly likes Class relations mess - OOP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Class relations mess - OOP

Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Hi guys,
I am a bit of java newby and I need a assistance. The problem is:

I have 3 classes:

1. In class A there is instance of class B (Class A is the main class)
2. In class B there is instance of class C (class B implements GUI)
3. I need in class C some components from class B (in class C I have computations and set and get methods)

How should I do this in good programming manner ? I tried to make instance of class B in class C, but I get a StackOverFlow error, so suppose that is a wrong way. Actually I need in set method in class C to set value from TextField which is in class B. When I want to take that values, does that discomfit encapsulation ?

I hope it is not too confusing. Any help is welcome! Thanks!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Without knowing more it sounds like one or more of your classes may be trying to do too much.

You're getting a stack overflow because the construction of one object creates an instance of another, which in turn creates an instance of the first, ad nauseum until the computer vomits.

There are a lot of solutions, which to use depends on your requirements. You could consider breaking up class responsibilities more granularly, just duplicating the fields you need, passing an instance of the one class into the other as a constructor parameter or setting it on the object after it's created, and so on.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
The simplest way is to have a B variable in C and then set the B variable either from a parameter passed into C's constructor or with a setB(B b) method. There are better ways, but this is a start.

For example:
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Thank you very much for quick responses!

It is somehow possible to do at that way, but it is not working like it should, but still I am not sure is it good programming practice and encapsulation violation if I do that so to say two-way connection, like in previous post ?

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Nicol Green wrote:Thank you very much for quick responses!

It is somehow possible to do at that way, but it is not working like it should,

hm, a small program that compiles and runs and demonstrates the issue would probably go a long way toward helping us find out what's not working.


but still I am not sure is it good programming practice and encapsulation violation if I do that so to say two-way connection, like in previous post ?

I don't see it as an encapsulation violation, but more of an issue of tight coupling and poor cohesion. Better perhaps would be to use interfaces and appropriate Design Patterns to loosen coupling and increase cohesion.
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Ok! Here are the classes:

Class Operations:


In class B:

I wanted in doOperations(operatorEn)instead of result = firstNumber + secondNumber; to say
result = firstNumber + Double.valueOf(textField.getText());
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Your code appears to be missing some critical things, such as class signatures for one. Perhaps someone can help you based on this information, but likely not me. If you don't get help soon, as I recommended above, please post small compilable classes that demonstrate your problem, code that we can compile on our systems without having to modify them, without requiring outside dependencies (such as images on disks or database). If you do this, you'll make it much easier for others to help you and will likely increase the speed at which you'll get a useful answer.

Much luck!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Nicol Green wrote:
...
I wanted in doOperations(operatorEn)instead of result = firstNumber + secondNumber; to say
result = firstNumber + Double.valueOf(textField.getText());


Ah, I can tell you this: that you really don't want to do this. doOperations is in your "model" while the textfield is in the "view" and in general, the model should have no dependencies on the view.

Again, if you post a simple app that does perhaps one operation, we can help you to better write this.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
For example, say my model, class MvcModel, is very simple -- only an add operation for illustrative purposes:



But note that the model has no references to the GUI, meaning it's not tied to any one GUI and it can be used by a Swing GUI, an AWT GUI, an Android GUI or whatever. Also, I can fiddle around with my GUI code and know that by doing this, I'm not going to break my model code.

The GUI/view, class MvcView, will be simple, just 3 JTextFields and a JButton:


it will also let me set the control on my button via a public method: setAddControl:


And also will have public methods to allow the control to extract the Strings from the two input fields, as well as a public method to allow the control to set the result field:


My control, MvcAddControl has a constructor that allows passage of a String, text, which is the button's text, an MvcView which is a reference the view object and an MvcModel param which is a reference to the model:


Then in its action peformed method, it puts it all together, extracting the Strings from the view's jtextfields, converting them to doubles, adding them (using the model) and setting the results into the view's results field:


I then have a main class, MvcMain creates a model, control and view object, ties them all together and then display's the view's main JPanel in a JFrame:


Putting it all together, we have:
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Pete, thank you very much!

Your posts were from great help!
I was just read it, because at the moment I am working at something else, but soon I will adjust it to my code, and let you know how it is working. I am really sorry, I was typing fast so some main parts unfortunately are missing in my last post.. I will be more careful.

I just have one more, maybe stupid question. Since I am a beginner..
Is there any rule, when I need some values for example, when I need to use method with return values, and when I need method with passing parameters ? Return values I use when I need some value from another method somewhere else, but with passing parameters I am getting stuck.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36497
    
  16
Depends on the method. If your method needs information sent to it, the receiving method calls that its parameters (and the sending method calls that arguments).

If you look through the javadoc for a class (let's try String which was the first one to come to hand), click METHOD, and look at the return types in the left column, which tell you what each method returns. Notice you only ever return one thing (or nothing, in which case it says void, but String hasn't got many void methods).
Notice that where it gives the method heading, there are things in the (). Each of those represents a parameter, so you have to provide arguments of the same type and number when you call those methods. Note some of them specify a range; try passing -1 to a method taking an int parameter and see what happens.

When you write your methods, you will put the parameter types and what you are calling themfor this method in the () after the method name. And you put the return type (or void) to the left of the method name. You have to decide what pieces of information your method needs, and that is how many parameters you write.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36497
    
  16
And about return types, you might find this thread about "void" useful. Or maybe not.
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
The text about void was useful, thanks!

I understand concept of class relations in Your example. But now I have a bit different situation, but it is somehow the same question.

I have class with GUI implemented and there I have a 3 text area (for plainText, for keyword and for cipherText)encryption button. I would like to implement Playfair and Vigenere cipher on on click event. But Playfair and Vigenere are separated classes, with a few methods. How do I do that connection between them ? Classes follows:


Any help will be appreciated, and Thank You in advance!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I think you should continue this in your other thread rather than resurrecting this old one.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36497
    
  16
David Newton wrote:I think you should continue this in your other thread rather than resurrecting this old one.
In that case I shall close this thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class relations mess - OOP
 
Similar Threads
Exam questions on inner classes -help!
coupling is lowered, and cohesion is increased
Failed SCEA - short by one point
creating an inner class
double indirection