Note that there are some explanatory texts on larger screens.

plurals
  1. POInsecure how to refactor repeated code
    primarykey
    data
    text
    <p>I'm trying to refactor an old legacy java app, and I have detected code that is repeated a lot of times in the project. I'm trying to place this code in only one place, but I don't know the best solution.</p> <p>The pseudocode for my problem is more or less the following:</p> <pre><code>public class MYDialog extends JDialog{ private VerySpecialHardwareDevice my_device; private int variable1; private int variable3; private int variableXX; private PropertyChangeListener my_listener=new PropertyChangeListener( { //The code of the listener interacts with the swing elements of the dialog //depending on the behaviour of the hardware device, and its //different most of the Jdialogs/Jpanels, and its the same in a few of them. }; public MyDialog(){ my_device=new VerySpecialHardwareDevice(); my_device.addPropertyChangeListener(my_listener); } /*Begin of the repeated method related with the special device*/ private void methodRelatedWithSpecialHardwareDevice1(){ //These methods and functions use some variables like variable 1, 2, etc }; private void methodRelatedWithSpecialHardwareDevice2(){...}; private void methodRelatedWithSpecialHardwareDevice3(){...}; (...) private void methodRelatedWithSpecialHardwareDevice15(){...}; /*************************************************************/ } public class MYPanel extends JPanel{ private VerySpecialHardwareDevice my_device; private int variable1; private int variable3; private int variableXX; private PropertyChangeListener my_listener=new PropertyChangeListener( { //.... }; public MyPanel(){ my_device=new VerySpecialHardwareDevice(); my_device.addPropertyChangeListener(my_listener); } /*Begin of the repeated method related with the special device*/ private void methodRelatedWithSpecialHardwareDevice1(){ //These methods and functions use some variables like variable 1, 2, etc }; private void methodRelatedWithSpecialHardwareDevice2(){...}; private void methodRelatedWithSpecialHardwareDevice3(){...}; (...) private void methodRelatedWithSpecialHardwareDevice15(){...}; /*************************************************************/ } </code></pre> <p>As you can see, the methods are related to a hardware device. The method from 1 to 15 are always the same, and this device is used in a lot of different Jdialogs and Jpanels accross the aplication (more than twenty forms). Each Jdialog has a property change listener which interacts with the gui depending on what the hardware device does. Most of the times these change listener are equal, but there are a few exceptions.</p> <p>My first intention was to move all this code (except the listener) to a superclass, but I'm insecure because I don't know wich superclass is the best for a Jdialog and a Jpanel at once...</p> <p>Maybe the is a better aproach to avoid this code repetition.</p> <p>Please excuse my bad ENglish and excuse me if this question has an obvious answer.</p> <p>Best regards</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload