GoF 디자인패턴 - Mediator

Clean Code Big Poo·2025년 6월 3일
0
post-thumbnail

Overview

디자인패턴 시작하기를 보고 정리하기

Mediator

중재자 패턴,


여기 객체가 관계가 밀접함. 객체가 추가될 수록 관계에 대한 복잡도가 증가함.


각 객체들은 서로 관게를 맺지 않고, 중재자와만 관계를 맺고, 자신의 상태에 대한 정보만 업데이트 해준다. 중재자는 이 상태를 감지하고 다른 객체에 전달한다.

다이어그램

  • Participant : 각 객체들의 부모 클래스
    • 중재자(Madiator)를 참조
    • 자식 클래스의 변경이 일어나면 중재자에게 알리는 역할을 함.
  • SmartHome : 중재자를 구현함.

구현 코드

Madiator

public interface Mediator {
    void participantChanged(Participant participant);
}

Participant

public abstract class Participant {
    protected Mediator mediator;

    public Participant(Mediator mediator){
        this.mediator = mediator;
    }
}

Door

public class Door extends Participant{
    private boolean isCloesd  = true;

    public Door(Mediator mediator) {
        super(mediator);
    }

    public void open(){
        if(!isCloesd) return;

        isCloesd = false;
        mediator.participantChanged(this);
    }

    public void close(){
        if(isCloesd) return;

        isCloesd = true;
        mediator.participantChanged(this);
    }

    public boolean isClosed(){
        return this.isCloesd;
    }

    public String toSring(){
        if(isCloesd) return  "# 문 닫힘";

        return "# 문 열림";
    }
}

Window

public class Window extends Participant{
    private boolean isCloesd  = true;

    public Window(Mediator mediator) {
        super(mediator);
    }

    public void open(){
        if(!isCloesd) return;

        isCloesd = false;
        mediator.participantChanged(this);
    }

    public void close(){
        if(isCloesd) return;

        isCloesd = true;
        mediator.participantChanged(this);
    }

    public boolean isClosed(){
        return this.isCloesd;
    }

    public String toSring(){
        if(isCloesd) return  "# 창문 닫힘";

        return "# 창문 열림";
    }
}

CoolAircon

public class CoolAircon extends Participant {
    private boolean bOff = true;

    public CoolAircon(Mediator mediator) {
        super(mediator);
    }

    public void on() {
        if (!bOff) return;

        bOff = false;
        mediator.participantChanged(this);
    }

    public void off() {
        if (bOff) return;

        bOff = true;
        mediator.participantChanged(this);
    }

    public boolean isRunning() {
        return !bOff;
    }

    
    public String toString() {
        if (bOff) return "# 에어컨: 꺼짐";
        else return "# 에어컨: 켜짐";
    }
}

HeatBoiler

public class HeatBoiler extends Participant {
    private boolean bOff = true;

    public HeatBoiler(Mediator mediator) {
        super(mediator);
    }

    public void on() {
        if (!bOff) return;

        bOff = false;
        mediator.participantChanged(this);
    }

    public void off() {
        if (bOff) return;

        bOff = true;
        mediator.participantChanged(this);
    }

    public boolean isRunning() {
        return !bOff;
    }

    
    public String toString() {
        if (bOff) return "# 보일러: 꺼짐";
        else return "# 보일러: 켜짐";
    }
}

SmartHome

public class SmartHome implements Mediator {
    public Door door = new Door(this);
    public Window window = new Window(this);
    public CoolAircon aircon = new CoolAircon(this);
    public HeatBoiler boiler = new HeatBoiler(this);

    // 참여자 간의 관계는 자주 변경될 수 있으므로, 한 곳에 집중하여 구현하는 것이 유지보수에 용이
    
    public void participantChanged(Participant participant) {
        if (participant == door && !door.isClosed()) {
            aircon.off();
            boiler.off();
        }

        if (participant == window && !window.isClosed()) {
            aircon.off();
            boiler.off();
        }

        if (participant == aircon && aircon.isRunning()) {
            boiler.off();
            window.close();
            door.close();
        }

        if (participant == boiler && boiler.isRunning()) {
            aircon.off();
            window.close();
            door.close();
        }
    }

    public void report() {
        System.out.println("\033[H\033[2J[집안 상태]");
        System.out.println(door);
        System.out.println(window);
        System.out.println(aircon);
        System.out.println(boiler);
        System.out.println();
    }
}

Main


import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        SmartHome home = new SmartHome();

        try (Scanner scanner = new Scanner(System.in)) {
            do {
                home.report();

                System.out.println("[1] 문 열기");
                System.out.println("[2] 문 닫기");
                System.out.println("[3] 창문 열기");
                System.out.println("[4] 창문 닫기");
                System.out.println("[5] 에어켠 켜기");
                System.out.println("[6] 에어켠 끄기");
                System.out.println("[7] 보일러 켜기");
                System.out.println("[8] 보일러 끄기");

                System.out.print("명령: ");
                int i = scanner.nextInt();

                if (i == 1) home.door.open();
                else if (i == 2) home.door.close();
                else if (i == 3) home.window.open();
                else if (i == 4) home.window.close();
                else if (i == 5) home.aircon.on();
                else if (i == 6) home.aircon.off();
                else if (i == 7) home.boiler.on();
                else if (i == 8) home.boiler.off();
            } while (true);
        }
    }
}

결과

0개의 댓글