본문 바로가기

디자인패턴

[디자인패턴] 퍼사드 패턴(Facade Pattern)

정의

Facade는 "건물의 정면"을 의미하는 단어로 어떤 소프트웨어의 다른 커다란 코드 부분에 대하여 간략화된 인터페이스를 제공해주는 디자인 패턴을 의미합니다. 
퍼사드 객체는 복잡한 소프트웨어 바깥쪽의 코드가 라이브러리의 안쪽 코드에 의존하는 일을 감소시켜 주고, 복잡한 소프트웨어를 사용 할 수 있게 간단한 인터페이스를 제공해줍니다.

 

예시) 

여러 클래스의 객체들을 복합적으로 사용해야 수행할 수 있는 작업들이 있다.

이러한 복잡한 작업들이 여러곳에서 이루어질 때,  복잡한 수행과정을 하나의 클래스의 메소드로 만들어 구현한다.

사용자로 하여금 복잡한 연관 관계를 알 필요가 없도록 구현부를 프랑스어로 '외벽' 을 뜻하는 Facade 뒤에 숨겨둔다.

 

즉, 복잡하고 반복되는 코드를 하나의 메소드로 만들어 재사용!

 

 

 

1
2
3
4
5
6
7
public class GeoLocation {
    public double[] getGeoLoc() {
        double[] geoLoc = {00};
        return geoLoc;
    }
}
 
cs
1
2
3
4
5
6
7
8
public class InternetConnection {
    public void connect() {}
        public String getData(String url, Object param) {
        return "";
    }
    public void disconnect() {};
}
 
cs
1
2
3
4
5
6
7
public class Json {
    public Map<String, Object> parse(String str) {
        Map<String, Object> result = new HashMap<>();
        result.put("address""서울시 개발구 객체동");
        return  result;
    }
}
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MyLocFacade {
    public void printMyAddress() {
        double[] myGeoLoc = new GeoLocation().getGeoLoc();
 
        InternetConnection conn = new InternetConnection();
        conn.connect();
        String data = conn.getData("https://주소_API_URL", myGeoLoc);
        conn.disconnect();
 
        Map<String, Object> address = new Json().parse(data);
 
        System.out.println(address.get("address"));
    }
}
 
cs
1
2
3
4
5
public class FacadepPatternExample {
    public static void main(String[] args) {
        new MyLocFacade().printMyAddress();
    }
}
cs

 

 

여러개의 객체의 메소드들이 섞인 과정을 하나의 메소드로 만들어 사용자로 하여금 Facade 뒤의 복잡한 과정에 관심을 없앤다.