N
N
nano_e_t_42016-11-23 20:44:35
Java
nano_e_t_4, 2016-11-23 20:44:35

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;
}

confuses the fact that I want to do everything according to feng shui, and write a class in which to add the logic of creating buttons and return not quite object. Or am I wrong, and is this approach generally the norm in OOP?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Soshnikov, 2016-11-23
@artem90

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 question

Ask a Question

731 491 924 answers to any question