All the programming books that I read perfectly reveal the technical details of writing code. For example, that if there are abstract methods in the parent class, then in the derived class they must be redefined or this class itself must be abstract. Or the theory is given, I read several books where the principles of the PLO were perfectly explained. This is all very good and important. But neither books nor in the university teach how to write programs (my opinion). I will give an example: I am writing a GUI. I have a method in which I set properties for a JInternalFrame, customize its appearance. And then I call this method for each JInternalFrame. Here he is:

private static void createIFrameProperties(JInternalFrame iframe) { // TODO Auto-generated method stub BasicInternalFrameUI ui = new BasicInternalFrameUI(iframe); iframe.setUI(ui); ((BasicInternalFrameUI) iframe.getUI()).getNorthPane() .setPreferredSize(new Dimension(400, 28)); ((BasicInternalFrameUI) iframe.getUI()).getNorthPane().setBorder( new LineBorder(Color.GRAY, 1)); ((BasicInternalFrameUI) iframe.getUI()).getNorthPane().remove(0); ((BasicInternalFrameUI) iframe.getUI()).getNorthPane().remove(0); ((BasicInternalFrameUI) iframe.getUI()).getNorthPane().remove(0); ((BasicInternalFrameUI) iframe.getUI()).getNorthPane().revalidate(); ((BasicInternalFrameUI) iframe.getUI()).getNorthPane().repaint(); JComponent title = ((BasicInternalFrameUI) iframe.getUI()) .getNorthPane(); for (int i = 0; i < title.getComponentCount(); i++) { JComponent component = (JComponent) title.getComponent(i); if (component instanceof JButton) { JButton button = ((JButton) component); button.setToolTipText("Закрыть"); button.setMinimumSize(new Dimension(24, 24)); button.addMouseListener(new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub button.setIcon(new ImageIcon("images/closered.png")); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub button.setIcon(new ImageIcon("images/closeEnd.png")); } }); button.setIcon(new ImageIcon("images/closeEnd.png")); button.setSelectedIcon(new ImageIcon("images/closeEnd.png")); button.setPressedIcon(new ImageIcon("images/closeEnd.png")); } } iframe.addInternalFrameListener(new InternalFrameAdapter() { @Override public void internalFrameClosed(InternalFrameEvent e) { frameForRead = null; frameForHelp = null; } @Override public void internalFrameClosing(InternalFrameEvent e) { frameForRead = null; frameForHelp = null; } }); } 

Or, you can create a class, inherit it from JInternalFrame, and register it all in its constructor. Like this:

 public class MyJInternalFrame extends JInternalFrame { private boolean isOpened; public MyJInternalFrame(String s) { super(s, true, true, false, false); BasicInternalFrameUI ui = new BasicInternalFrameUI(this); setUI(ui); ((BasicInternalFrameUI) getUI()).getNorthPane() .setPreferredSize(new Dimension(400, 28)); ((BasicInternalFrameUI) getUI()).getNorthPane().setBorder( new LineBorder(Color.GRAY, 1)); ((BasicInternalFrameUI) getUI()).getNorthPane().remove(0); ((BasicInternalFrameUI) getUI()).getNorthPane().remove(0); ((BasicInternalFrameUI) getUI()).getNorthPane().remove(0); ((BasicInternalFrameUI) getUI()).getNorthPane().revalidate(); ((BasicInternalFrameUI) getUI()).getNorthPane().repaint(); createNorthPanel(); setVisible(true); setMinimumSize(new Dimension(400, 500)); } private void createNorthPanel() { // TODO Auto-generated method stub JComponent title = ((BasicInternalFrameUI) this.getUI()) .getNorthPane(); for (int i = 0; i < title.getComponentCount(); i++) { JComponent component = (JComponent) title.getComponent(i); if (component instanceof JButton) { JButton button = ((JButton) component); button.setToolTipText("Закрыть"); button.setMinimumSize(new Dimension(24, 24)); button.addMouseListener(new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub button.setIcon(new ImageIcon("images/closered.png")); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub button.setIcon(new ImageIcon("images/closeEnd.png")); } }); button.setIcon(new ImageIcon("images/closeEnd.png")); button.setSelectedIcon(new ImageIcon("images/closeEnd.png")); button.setPressedIcon(new ImageIcon("images/closeEnd.png")); } } } 

Then in the program you can simply create objects of this class and they will already be properly configured. The same goes for buttons and all other controls. It is possible to register everything in one class in which the GUI is created. This class will work out for me about 3000 lines. Or you can spread it into several classes (as in the example), then the main class will be much less. That is, the question is: what is better, one class for 3,000 lines or 20 for 200 lines each (numbers are taken approximately)? PS I know about patterns, but in my opinion this is a question from another area. Rather, I wonder how to write code more professionally or something.

  • Analyze the result. For speed, for memory consumption. Conduct experiments. Do you think that now some cool expert will come out and tell you how to write the code correctly? Not! Read someone else's code right here in the answers. See the code for large projects on GitHub. This uncertainty will pass with experience. - LEQADA
  • In short, it is better than 20 classes of 200 lines. - MichaelPak
  • @MichaelPak, Still, 20 to 200 is better? Even despite the fact that it turns out 4000 instead of 3000? Can you explain why? I see one reason - a class in which 3000 lines are very hard to keep and maintain. I heard of course that each class should be responsible for only one task. But here it would be respected, even if there was one class in 3000. Although it would be huge, it would only perform one task (creating a GUI). So are there any other reasons in this case to spread one class to 20, besides improving readability? - Alexander Elizarov

1 answer 1

No, all the same design patterns are just what you need. In your case, I would use such a pattern as a builder ( article with habrahabr ). But if after acquaintance you are still not satisfied, then I suggest reading Steve McConnell's “Perfect Code” .

  • Thank you very much! I didn’t quite understand the builder, probably I still don’t have enough programming experience. But now I know which way to go. Tell me more please, does the Effective Java Bloch book and Thinking in java by Ekel help you move in this direction? In the sense of writing more professional code (I heard such reviews about them)? Or do they also focus more on technical details? - Alexander Elizarov
  • one
    @AlexanderElizarov, I suggest that you first familiarize yourself with the philosophy of Eichel java (at least with the first chapters where the basics of the PLO are described), and then move on to the gang of four . And do not pay for such a stupid picture, a very good and understandable book! - MichaelPak 8:49
  • But what about the Russian translation of java philosophy? I heard different reviews. Is it generally possible to read? Or is it better still in English, despite the fact that it takes 3 times more time? - Alexander Elizarov
  • @Alexander Elizarov, people re-read it several times, since it is impossible to remember everything at once. So for the first time it is better to read in Russian. And I have not heard any bad reviews about the translation. - MichaelPak
  • one
    And the gang of four is like this book: ozon.ru/context/detail/id/2457392 or am I confusing? - Alexander Elizarov