I have being implementing a client in Java as application with Swing. But now I want to build also Applet from it. What is the best way to redesign/refactor in order to be able to build both of them easily and keeping it DRY.
This is a short extraction of code which has main()
public class Client {
public static final ScheduledExecutorService SERVICE;
protected static String host;
protected static int port;
static {
SERVICE = Executors.newSingleThreadScheduledExecutor();
host =
port =
}
public static void main(String[] args) {
//initalize netty
//create user interface = JFrame in SwingUtilities.invokeLater
connect();
}
public static void connect () {
//connect using netty
}
So I copy this file as a separate one, extend it from JApplet
and change main
to init
, so it can be run, but of course it is ugly, because much of code is just copy-pasted.
Is there universal solution how to redesign it?
UPD:
public class Client {
public static void main (String[] args) {
App app = new App();
app.connect();
}
}
public class Applet extends JApplet {
public void init () {
App app = new App();
app.connect();
}
}
and to move all initialization logic to App