SELF STUDY/Design Pattern

[디자인패턴] 퍼사드 패턴 | Facade Pattern | 안드로이드 예제

호이호이호잇 2021. 12. 27. 19:27
728x90
반응형

 

오랜만에 또 디자인패턴.!

 

오늘은 퍼사드 패턴을 해보려고 한다.

 

먼저, 위의 펭수처럼 출근을 하려면 필요한 단계가 있다.

나의 경우에는 알람으로 기상 -> 씻기 -> 옷입기 -> 전철을 타서 출근을 하는 단계를 거친다.

 

각 단계를 코드로 보면..

 

"알람"

- 알람을 듣고 일어나거나.. 그냥 꺼버리는 경우.. ㅠ

class Alarm {
    public void Wake(TextView tv){
         tv.setText( (String) tv.getText() + "\nwake!" );
    }

    public void OFF(TextView tv){
        tv.setText( (String) tv.getText() + "\nalarm off!" );
    }
}

 

"씻기"

- 씻고 가거나 그냥 안씻고 가는 경우..

class Wash {
    public void Shower(TextView tv){
        tv.setText( (String) tv.getText() + "\nshower!" );
    }

    public void Nothing(TextView tv){
        tv.setText( (String) tv.getText() + "\nNothing!" );
    }
}

 

"옷입기"

class Clothes {
    public void Wear(TextView tv){
        tv.setText( (String) tv.getText() + "\nwear!" );
    }
}

 

"이동수단"

- 버스 / 자동차 / 걷기 

class Vehicle {
    public void Bus(TextView tv){
        tv.setText( (String) tv.getText() + "\nbus!" );
    }

    public void Car(TextView tv){
        tv.setText( (String) tv.getText() + "\ncar!" );
    }

    public void Walk(TextView tv){
        tv.setText( (String) tv.getText() + "\nwalk!" );
    }
}

 

이렇게로 표현이 가능하다고 생각한다.

 

이제 이러한 각 단계를 사용자가 하나하나 불러서 사용하면 된다.

alarm.Wake(tv);
wash.Shower(tv);
clothes.Wear(tv);
vehicle.Bus(tv);

만약 이 준비 단계가 매번 반복되는 정해져있는 시나리오? 라면.. 하나하나 불러서 매번 사용해주는 것은 번거롭게 느껴질 수 있다.

 

이때 퍼사드 패턴을 사용하면 좋다.


퍼사드 패턴이란?

 

서브 시스템의 일련의 인터페이스에 대한 통합된 인터페이스를 제공하는 것이다.

즉, 서브시스템을 더 쉽게 사용 할 수 있도록 하는 패턴.


 

퍼사드 패턴이 적용된 클래스를 하나 만들면..

class ReadyToWork{
    Alarm alarm;
    Wash wash;
    Clothes clothes;
    Vehicle vehicle;

    public ReadyToWork() {
        alarm = new Alarm();
        wash = new Wash();
        clothes = new Clothes();
        vehicle = new Vehicle();
    }

    public void start(TextView tv) {
        this.alarm.Wake(tv);
        this.wash.Shower(tv);
        this.clothes.Wear(tv);
        this.vehicle.Bus(tv);
    }
}

이런 클래스를 하나 만들 수 있다.

 

즉 사용자는

readyToWork.start(textView); 만 사용해주면 된다.

 


이 퍼사드 클래스에는 한 가지 원칙이 적용되어야 한다.

바로바로

최소 지식 원칙 (=테메데르 법칙) 이다.

이 법칙은 퍼사드 패턴이 적용된 클래스에서 사용할 수 있는 외부 class에 대한 정의를 하고 있다. 

사용할 수 있는 외부 클래스는

1. 객체 자체

   ex) ReadToWork

2. 메소드에 매개 변수로 전달 된 것.

   ex) ReadyToWork(Wash wash

3. 메소드에서 생성하거나 인스턴스를 만든 객체

   ex) public void start() {
           Wash wash = new Wash();

           wash.shower();
       }

4. 객체에 속하는 구성 요소

   class ReadyToWork{
       Wash wash;

   }

이다.

 

이에 주의해서 퍼사드 클래스를 생성하는 해야한다.!!


사용자의 전체 소스를 보면..!

    TextView textView;
    ReadyToWork readyToWork;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facade);

        init();
    }

    private void init(){
        findViewById(R.id.buttonToReady).setOnClickListener(this);
        textView = findViewById(R.id.textView);
        readyToWork = new ReadyToWork();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonToReady:
                readyToWork.start(textView);
                break;
        }
    }

가 된다.

 

전체 소스는

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class FacadeActivity extends Activity implements View.OnClickListener {

    TextView textView;
    ReadyToWork readyToWork;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facade);

        init();
    }

    private void init(){
        findViewById(R.id.buttonToReady).setOnClickListener(this);
        textView = findViewById(R.id.textView);
        readyToWork = new ReadyToWork();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonToReady:
                readyToWork.start(textView);
                break;
        }
    }
}

class Alarm {
    public void Wake(TextView tv){
         tv.setText( (String) tv.getText() + "\nwake!" );
    }

    public void OFF(TextView tv){
        tv.setText( (String) tv.getText() + "\nalarm off!" );
    }
}

class Wash {
    public void Shower(TextView tv){
        tv.setText( (String) tv.getText() + "\nshower!" );
    }

    public void Nothing(TextView tv){
        tv.setText( (String) tv.getText() + "\nNothing!" );
    }
}

class Clothes {
    public void Wear(TextView tv){
        tv.setText( (String) tv.getText() + "\nwear!" );
    }
}

class Vehicle {
    public void Bus(TextView tv){
        tv.setText( (String) tv.getText() + "\nbus!" );
    }

    public void Car(TextView tv){
        tv.setText( (String) tv.getText() + "\ncar!" );
    }

    public void Walk(TextView tv){
        tv.setText( (String) tv.getText() + "\nwalk!" );
    }
}

class ReadyToWork{
    Alarm alarm;
    Wash wash;
    Clothes clothes;
    Vehicle vehicle;

    public ReadyToWork() {
        alarm = new Alarm();
        wash = new Wash();
        clothes = new Clothes();
        vehicle = new Vehicle();
    }

    public void start(TextView tv) {
        this.alarm.Wake(tv);
        this.wash.Shower(tv);
        this.clothes.Wear(tv);
        this.vehicle.Bus(tv);
    }
}

 

테스트 화면 : 

버튼을 누르면! 

순서대로 준비 한다!!

 

끄읐~~!

728x90
반응형