영회형의 최신글에서 JDBC 노가다를 CallBack을 사용해서 멋지게 처리하는 글을 보고 이런 형태로 개선하면 좋을 것 같은 부분들이 떠올랐습니다.

IO를 할 때 try-catch 블락이 계속 중복 되는데요. 아래의 코드를 보겠습니다.
[#M_ more.. | less.. |

    public void getWebSiteInfoHavingNewPosts(){
        request = "http://www.hanrss.com/api/list_subs_new_items.qst";

        try {
            URL url = new URL(request);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestProperty("Authorization", "Basic " +
                    new sun.misc.BASE64Encoder().encode(new String(id + ":" + password).getBytes()));
            br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = "";
            while ((line = br.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(builder.toString());
    }

 

  public void getRecentFivePostWithFSRL(){
        String fsrl="98000";
        request = "http://www.hanrss.com/api/get_recent_items.qst?fsrl=" + fsrl;

        try{
            URL url = new URL(request);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = "";
            while ((line = br.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(builder);
    }

  

public void getOnePostWithFSRLAndSSRL(){
        String fsrl="98000";
        String ssrl="388743";
        request = "http://www.hanrss.com/myfeeds_main.qst?fsrl=" + fsrl + "&ssrl=" + ssrl;

        try {
            URL url = new URL(request);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = "";
            while ((line = br.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(builder);
    }

_M#]첫번째 메소드만 중간에 뭐가 한 문장이 추가 됩니다. 다른 두 개의 메소드는 빨간색 글씨 아래로는 완전히 같은 코드입니다. 빨간 글씨 아래의 큰 덩어리를 다른 메소드로 분리해 내고 싶었지만 첫번째 메소드 처럼 중간에 connection에 뭔가를 추가해야할 일이 생길 수도 때문에 그렇게 하지 못하고 조금씩 묶어서 다른 메소드로 빼냈습니다.

그랬더니 코드가 아래처럼 됐습니다.
[#M_ more.. | less.. |

    public void getWebSiteInfoHavingNewPosts(){
        request = "http://www.hanrss.com/api/list_subs_new_items.qst";

        try {
            HttpURLConnection connection = getConnection();
            connection.setRequestProperty("Authorization", "Basic " +
                    new sun.misc.BASE64Encoder().encode(new String(id + ":" + password).getBytes()));
            makeResult(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(builder.toString());
    }

 

  public void getRecentFivePostWithFSRL(){
        String fsrl="98000";
        request = "http://www.hanrss.com/api/get_recent_items.qst?fsrl=" + fsrl;

        try{
            HttpURLConnection connection = getConnection();
            makeResult(connection);
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(builder);
    }

  

public void getOnePostWithFSRLAndSSRL(){
        String fsrl="98000";
        String ssrl="388743";
        request = "http://www.hanrss.com/myfeeds_main.qst?fsrl=" + fsrl + "&ssrl=" + ssrl;

        try {
            HttpURLConnection connection = getConnection();
            makeResult(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(builder);
    }

_M#]물론 메소드들이 다이어트를 해서 조금 보긴 괜찮은데 그래도 try-catch가 자꾸 중복되서 나오는게 보기 싫어졌습니다. CallBack을 만들어서 써보고 싶어졌습니다. 결국 코드는 아래처럼 변했습니다.

[#M_ more.. | less.. |    

public void getWebSiteInfoHavingNewPosts(){

        request = "http://www.hanrss.com/api/list_subs_new_items.qst";

        sendRequest(new ConnectionCallBack(){
            public void doSomethingWithConnection(HttpURLConnection connection) {
                connection.setRequestProperty("Authorization", "Basic " +
                    new sun.misc.BASE64Encoder().encode(new String(id + ":" + password).getBytes()));
            }
        });
        System.out.println(builder.toString());
    }

  

public void getRecentFivePostWithFSRL(){
        String fsrl="98000";
        request = "http://www.hanrss.com/api/get_recent_items.qst?fsrl=" + fsrl;

        sendRequest(new ConnectionCallBack(){});
        System.out.println(builder);
    }

 

  public void getOnePostWithFSRLAndSSRL(){
        String fsrl="98000";
        String ssrl="388743";
        request = "http://www.hanrss.com/myfeeds_main.qst?fsrl=" + fsrl + "&ssrl=" + ssrl;

        sendRequest(new ConnectionCallBack(){});
        System.out.println(builder);
    }

 _M#]흠.. interface로 해두니까 꼭 구현을 해야 되서 아래 두 개의 메소드에서는 구현할 필요가 없는데 구현을 해야만 하는 상황이 생겼네요. 그래서 interface를 class로 바꾸고 빈 메소드로 만들어 두었습니다.

으~ 어렵습니다... 마지막 코드도 별로 좋아보이지 않는데요. 흠...좀 더 공부하다 보면 좋은 방법이 생기겠죠.