Answer the question
In order to leave comments, you need to log in
What is correct from the point of view of OOP?
Hello everyone
I am writing a small application (relatively recently in OOP), I encountered such a logical problem:
I have a gui, it has a lot of buttons. So, in order not to abandon the class that builds this gui, I created a separate class buttons, where I actually create buttons, create and add listeners for none (not reflected in the example because it takes a long time to write. But almost every button has different ones) and add them to the frame . If in an example, then you can reflect it like this:
class mainGui {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
Buttons btns = new Buttons();
JButton but1 = btns.getFristButton("Puppy", but1);
JButton but2 = btns.getSecondButton("Citty", but2);
panel.add(but1);
panel.add(but2);
frame.add(panel);
=============================
class Buttons {
String Name;
String Type;
JButton getFristButton(String name) {
this.Name = name;
}
JButton getSecondButtons(String type) {
this.Type = type;
}
Answer the question
In order to leave comments, you need to log in
I would suggest the following approach:
- The form class, responsible for creating and placing objects on the form, also hangs all handlers.
- the dependency of the service class is injected into the form, which performs all the logic for selecting and saving, etc.
- If the form is too complex, then you can separate the creation of elements and the installation of handlers into two classes by type like View and Controller: one will only build the form, and the other will manage it.
I hope I understood your question correctly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question