2010-01-26 5 views
2

디자인 문제가 있습니다. 나는 다음 (관련) 클래스가 있습니다Http Requester의 Java 디자인에 대한 도움이 필요합니다.

class LoginScreen { 
    public login() { 
     httpRequest = factory.createHttpRequest(url, method, this); 
     httpRequest.start(); 
    } 
    public authorize() { 
     httpRequest = factory.createHttpRequest(url, method, this); 
     httpRequest.start(); 
    } 
    public requestSucceeded(){...} 
    public requestFailed(){...} 
} 

class HttpRequest extends Thread { 

    LoginScreen screen; 
    ... 

    public HttpRequest(url, method, screen) { 
     this.url = url; 
     this.method = method; 
     this.screen = screen 
    } 

    public run() { 
    ... 
    } 
} 

당신이 볼 수 있듯이, 모두 로그인을() 다음 코드를 실행) (허가 : 나사 HttpRequest를 클래스, 다음

httpRequest = factory.createHttpRequest(url, method, loginScreen); 
httpRequest.start(); 

을 그리고 HttpRequest 후에 run() 메서드는 LoginScreen을 결과와 함께 직접 업데이트합니다. 예 :

screen.requestSucceeded(baos.toByteArray(), contentType); 

그러나 이것은 매우 열악한 디자인임이 입증됩니다. 앞으로 HttpRequest 메서드 클래스를 재사용 할 수 있기를 바라고, 여러 가지 다른 유형의 클래스 나 스크린을 전달할 수 있기를 원합니다. 지금은 LoginScreen에서만 작동하며 LoginScreen UI를 직접 업데이트하고 있습니다.

누구나 내가이 디자인을 어떻게 업데이트 할 수 있는지에 대한 제안이 있습니까? 가능한 한 많은 세부 사항을 제공 할 수 있다면, 나는 많은 다른 것들을 시도해 보았고 나는 계속 문제에 부딪 히고 있습니다.

감사합니다.

답변

2

Observer pattern을 살펴보십시오. 이 경우 UI 화면은 옵저버가됩니다.

관련 문제