DESIGN PATTERN/실용주의 디자인패턴

Chapter 2 - Abstract factory 패턴 : 패턴 비빔밥

파란실버라이트 2012. 12. 20. 13:43

 

 

 URLConnection 클래스는 어디에 포커스를 두는가에 따라서 아래 2가지 역활 맡는다.

 1. URL Concreate Factory의 Abstract Production

 2. IntputStarem interface를 Create하는 Abstract Factory

 

interface URLConnection
{
   void setDoInput (boolean on);
   void setDoOutput (boolean on);
   void connect ();
  
   InputStream getInputStream();
}
 
 abstract class InputStream // used as interface
 {
    public abstract int read();
 }
 
 class URL
 {
    private String spec;
  
    public URL( String spec ){ this.spec = spec; }
  
    public URLConnection openConnection()
   {
      return new HttpURLConnection(this);
   }
 }
 
 class HttpURLConnection implements URLConnection
 {
   public HttpURLConnection(URL toHere) { /*...*/ }
   public InputStream getInputStream()
   {
      return new InputStream()
   {
  
   public int read()
   {

      // code goes here to read using the HTTP Protocol
      return -1;
   }
 };
 }
 
   public void setDoInput (boolean on) { /*...*/ }
   public void setDoOutput (boolean on) { /*...*/ }
   public void connect ( ) { /*...*/ }
 }

* 패턴을 오버랩하여 사용하여, 하나의 클래스가 여러 패턴에 참여하는 것은 흔한 일이다 관점의 전환에 익숙해지자