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!
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.
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 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.
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:
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.
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.
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!