mardi 31 mars 2015

Design pattern to remove duplicate code for Runnable interface

I have 2 classes with Runnable interfaces that are called when sending POST request to endpoints:



class Testinput implements Runnable {
private final String USER_AGENT = "Mozilla/5.0";
private static Enginetest enginetest;
private static MySqlite mySqlite = new MySqlite();
public Testinput(String v1,String v2, String urlpass) {
this.v1 = Integer.parseInt(v1);
this.v2 = Integer.parseInt(v2);
this.enginetest = new Enginetest();
this.urlpass = urlpass;
}

public void run(){
try{
enginetest.method2(v2, true);
List<String> res = enginetest.method1(v1);

String url = urlpass;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.addRequestProperty("content-type", "application/json");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

JSONObject json=new JSONObject();
json.put("result",res.get(0));

String urlParameters = json.toString();
System.out.println(urlParameters);

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
if (responseCode!=200){
System.out.println("failed");
}
else{
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
} catch (Exception e){
NotifyAdmin.notify_watching("NOt working", "TODO");
e.printStackTrace();
}
}
}


ANd the second Runnable class:



class Getaccount implements Runnable {
private final String USER_AGENT = "Mozilla/5.0";
private static Enginetest enginetest;
private static MySqlite mySqlite = new MySqlite();
public Getaccount(String vimvalue, String urlPass) {
this.vimvalue= Integer.parseInt(vimvalue);
this.valuetest = new Valuetest();
this.urlPass = urlPass;
}

public void run(){
try{
Getaccount.setvalue(vimvalue);

String url = urlPass
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.addRequestProperty("content-type", "application/json");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
if (responseCode!=200){
System.out.println("failed");
}
else{
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
} catch (Exception e){
NotifyAdmin.notify_watching("NOt working", "TODO");
e.printStackTrace();
}
}
}


So the difference between these 2 classes is different methods we call inside run method and also different methods in Runnable interface (Getaccount, Testinput).


Since I have multiple Runnable interfaces in my code, what would be the best design pattern to remove duplication between run methods.


Aucun commentaire:

Enregistrer un commentaire