1
\$\begingroup\$

I have the following java class which has many private variables and methods:

package gui;
import java.awt.Color;
import java.io.File;
import java.util.*;
import java.util.stream.Stream;
import javax.swing.*;
import messages.Message;
import net.miginfocom.swing.MigLayout;
import utils.*;
public class FileCopyManager extends JFrame {
    public static String appName  = "File Copy Manager v1.6.4.0";
    private PreferencesManager pManager             = new PreferencesManager(this);
    private JCheckBoxMenuItem allowDuplicatesOption = new JCheckBoxMenuItem("Allow dupliactes in list");
    private Controller controller = new Controller();
    private StatusFrame status    = new StatusFrame();
    private FileHandler fHandler  = new FileHandler(status);    
    private Message      msg      = new Message();
    private JMenuBar menuBar      = new JMenuBar();
    private JMenu   fileMenu      = new JMenu("File"),
                    editMenu      = new JMenu("Edit");
    private JMenuItem saveList    = new JMenuItem("Save queue"),
            exit                  = new JMenuItem("Exit"),
            loadList              = new JMenuItem("Load queue"),
            openAppDirectory      = new JMenuItem("Open app folder"),
            showPreferences       = new JMenuItem("Preferences"),
            exportPreferences     = new JMenuItem("Export Preferences"),
            deleteApp             = new JMenuItem("Delete app settings"),
            restartApp            = new JMenuItem("Restart Application");
        private File      selectedFile = null;
    private String destinationPath = null;
    private ArrayList<File> files  = new ArrayList<>();
    private JPanel panel = new JPanel();
    private JFileChooser chooser = new JFileChooser();
    private JButton addFiles, selectDestination, copyFile, copyFiles,
            deleteFile, deleteAll, openDestinationFolder,stopCopy;
    private JComboBox<String> fileNames;
    private DefaultComboBoxModel<String> model;
    private int selectedFileIndex = -1;
    private JTextField dragPanel = new JTextField(20);
    private JLabel dragLabel;
    private File listFile = new File("app"+PreferencesManager.sep+"userList.dat");
    private boolean allowDuplicates = false;
    private Thread[] copyThreads = new Thread[2];
    private boolean isNull(Object...t){
        /*
         * Check if any of the arguments is null
         */
    }
    private void allowCopy(){
        /*
         * Enables certain buttons based on certain criteria
         */
    }
    private void allowDelete(){
        /*
         * Enables certain buttons based on certain criteria
         */
    }
    private void allowEdits(){
        /*
         * Enables certain buttons based on certain criteria
         */
    }
    private void createList(){
        //Create a list with some random names
    }
    private void initDragAreas(){
        /*
        * Create some drag and drop ares in the GUI
        */
    }
    public void updateList(){
        // Update a lst in the CUI with some names
    }
    public void showFiles() {
        //Show some file names in the GUI
    }
    public void restart(){
        //Code that restarts the GUI
    }
    @SuppressWarnings("deprecation")
    private void initUIElements() {
        /***
        * Create some UI elements
        */
        initDragAreas();
    }
    public JLabel[] getLabels() {
        // return some labels
    }
    public JButton[] getButtons() {
        // return some buttons
    }
    public void preload() {
        /*
        * See if we need to change the main UI(change colors or font size)
        * and if we need do it first and then show the app
        */
    }
    public FileCopyManager) {
        initUIElements();
        preload();
    }
}

This application is a small project I made for fun during summer. I was wondering: Is it a bad thing to use too many private members and methods?

Would it be bad for a production code?

\$\endgroup\$
3
  • \$\begingroup\$ What makes you think private methods are bad? \$\endgroup\$
    – Mast
    Sep 2, 2016 at 11:16
  • \$\begingroup\$ I am just asking \$\endgroup\$
    – cssGEEK
    Sep 2, 2016 at 11:20
  • \$\begingroup\$ too many private methods are bad. But many private methods isn't it. \$\endgroup\$
    – Jens
    Sep 2, 2016 at 11:34

1 Answer 1

1
\$\begingroup\$

You are putting all in one class. That makes structuring and rereading hard. Hence your question, and maybe you felt the urge to make sections in the class by something like // ---------------------.

Then it is time to look at the Model-View-Controller paradigm. An internet search will give better material on this subject than I can.

Basically you have one instance of a Controller class, that is more or less accessible from everywhere. There are View classes (the JFrame) and the Model classes, the data model, directory lists.

The Models can be developed independently, and used in a library.

The Controller is listener of views and models, and controls views. It holds the data model and the view (JFrame).

The Views signal events to the controller. And provide hooks for the controller. They get passed data, the model.

So you can have several classes, in several subpackages.

The approach is:

  • Start a new project
  • Use a version control system, like GIT (has the repository with the project)
  • An empty controller that starts the JFrame with itself
  • The JFrame, the original code

And then move the data to the outside, handle in the Controller. Data changes via events over the controller. Business logic like enabling/disabling in the controller.

Then you might be able to split up the GUI in specific JPanels or such.

Instead of doing the same actionPerformed for a menu item and button, you might use an Action extending AbstractAction, and add that to the constructors of JMenuItem and JButton. That action might then be disabled/enabled instead of doing it to the components. It also means that you need not make all components available as fields, for instance all those JMenuItems.

For the rest, production quality, for me means, using a build infrastructure like maven, that handles for you the usage of third party libraries, versioning and so on.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.