flagtags / OOP-study

0 stars 0 forks source link

상태패턴 #28

Closed j03y14 closed 3 months ago

j03y14 commented 3 months ago

모든 뽑기 기계의 재고와 현재 상태를 알려주는 기능을 추가한다.

코드 알맹이 개수를 알려주는 getCount, 기계의 현재 상태를 알려주는 getState 는 이미 만들어져 있다.

보고서를 출력하고, 각 뽑기 기계의 위치를 알려주는 필드만 추가하면 된다.

abstract class State {
    abstract insertQuarter(): void;
    abstract ejectQuarter(): void;
    abstract turnCrank(): void;
    abstract dispense(): void;
}

class SoldOutState extends State {
    constructor(private gumballMachine: GumbalMachine) {
        super();
    }

    insertQuarter() {
        console.log('매진되었습니다');
    }

    ejectQuarter() {
        console.log('동전을 넣지 않으셨습니다. 동전이 반환되지 않습니다');
    }

    turnCrank() {
        console.log('매진되었습니다');
    }

    dispense() {
        console.log('매진되었습니다');
    }
}

class GumbalMachine {

    private state: State;

    private count = 0;

    private location: string;

    constructor(count: number, location: string) {
        this.count = count;
        // 그냥 이렇게 가정.
        this.state = new SoldOutState(this);

        this.location
    }

    getCount() {
        return this.count;
    }

    getState() {
        return this.state;
    }

    getLocation() {
        return this.location;
    }

}

class GumballMonitor {
    gumballMachine: GumbalMachine;

    constructor(gumballMachine: GumbalMachine) {
        this.gumballMachine = gumballMachine;
    }

    report() {
        console.log('뽑기 기계 보고서');
        console.log('현재 재고: ' + this.gumballMachine.getCount());
        console.log('현재 상태: ' + this.gumballMachine.getState());
        console.log('현재 위치: ' + this.gumballMachine.getLocation());
    }
}

const main = () => {
    const gumballMachine = new GumbalMachine(5, '서울');
    const monitor = new GumballMonitor(gumballMachine);

    monitor.report();
}

그런데 네트워크를 이용해 원격으로 모니터링을 하고 싶다. => GumballMonitor 클래스는 그대로 두고 원격 객체의 프록시만 넘기면 된다?

프록시는 진짜 객체를 대신하는 역할을 맡는다. 실제로는 네트워크로 멀리 떨어져있는 진짜 GumballMachine 클래스와 데이터를 주고받는다.

원격 프록시의 역할

원격 프록시는 원격 객체의 로컬 대변자 역할을 한다. 클라이언트 객체는 원격 객체의 메서드를 호출하는 것처럼 행동하지만 실제로는 프록시 객체의 메서드를 호출한다. 네트워크 통신과 같은 작업은 프록시 객체에 캡슐화 되어있다.

RMI?

자바스크립트에서 사용하려면 grpc 같은걸 사용해야 되나?

프록시 패턴의 정의

프록시 패턴은 특정 객체로의 접근을 제어한느 대리인을 제공한다.

interface GumbalMachine {
    getCount(): number;
    getState(): State;
    getLocation(): string;
}

class GumbalMachineRemote implements GumbalMachine {
    private state: State;

    private count = 0;

    private location: string;

    constructor(count: number, location: string) {

        this.count = count;
        // api 호출
        this.state = new SoldOutState(this);

        this.location
    }

    getCount() {
        // api 호출
        return this.count;
    }

    getState() {
        // api 호출
        return this.state;
    }

    getLocation() {
        // api 호출
        return this.location;
    }
}

class GumballMonitor {
    gumballMachine: GumbalMachine;

    constructor(gumballMachine: GumbalMachine) {
        this.gumballMachine = gumballMachine;
    }

    report() {
        console.log('뽑기 기계 보고서');
        console.log('현재 재고: ' + this.gumballMachine.getCount());
        console.log('현재 상태: ' + this.gumballMachine.getState());
        console.log('현재 위치: ' + this.gumballMachine.getLocation());
    }
}

const main = () => {
    const gumballMachine = new GumbalMachineRemote(5, '서울');
    const monitor = new GumballMonitor(gumballMachine);

    monitor.report();
}

main();