상태 패턴 : 

상태 패턴은 객체 내부 상태에 따라 행동이 달라지는 문제를 
해결하기 위한 패턴 입니다.
즉, 런타임에서 객체의 상태에 따른 메서드 호출이 달라질 때
상태패턴을 사용합니다.

객체 내부 상태에 따라 행동이 달라지고, 행동 후 상태가 
바뀔 수 있는 로직에 적합합니다.

 

예를들어 상태에 따라 달라지는 LightState가 있다고 하고,

그것을 다루는 TrafficLight가 있다고 하고 코드를 작성해보겠습니다 !

 

#ifndef TRAFFICLIGHT_H
#define TRAFFICLIGHT_H

#include <memory>

class TrafficLight;

// 상태 인터페이스
class ITrafficLightState {
public:
    virtual ~ITrafficLightState() {}
    virtual void EnterState(TrafficLight* trafficLight) = 0;
    virtual void HandleTimer(TrafficLight* trafficLight) = 0;
    virtual void Update(TrafficLight* trafficLight) = 0;
};

class TrafficLight {
public:
    TrafficLight();
    ~TrafficLight() = default;

    void SetState(std::unique_ptr<ITrafficLightState> newState);
    void HandleTimer();
    void Update();

private:
    std::unique_ptr<ITrafficLightState> currentState;
};

#endif

 

#include "TrafficLight.h"
#include <iostream>
#include "RedState.h" 

TrafficLight::TrafficLight() {
    SetState(std::make_unique<RedState>());
}

void TrafficLight::SetState(std::unique_ptr<ITrafficLightState> newState) {
    currentState = std::move(newState);
    currentState->EnterState(this);
}

void TrafficLight::HandleTimer() {
    currentState->HandleTimer(this);
}

void TrafficLight::Update() {
    currentState->Update(this);
}

 

그리고 State에 따른 Update,HandleTimer,EnterState를 작성해주겠습니다 

헤더파일에서는 Override만 사용하였기 때문에 Cpp파일로 대체하겠습니다 

#include "RedState.h"
#include <iostream>
#include "GreenState.h" 

void RedState::EnterState(TrafficLight* trafficLight) {
    std::cout << "Traffic Light is now RED. Stop!" << std::endl;
}

void RedState::HandleTimer(TrafficLight* trafficLight) {
   
    trafficLight->SetState(std::make_unique<GreenState>());
}

void RedState::Update(TrafficLight* trafficLight) {
    std::cout << "Red Light is active." << std::endl;
}

 

#include "GreenState.h"
#include <iostream>
#include "YellowState.h" 

void GreenState::EnterState(TrafficLight* trafficLight) {
    std::cout << "Traffic Light is now GREEN. Go!" << std::endl;
}

void GreenState::HandleTimer(TrafficLight* trafficLight) {
    trafficLight->SetState(std::make_unique<YellowState>());
}

void GreenState::Update(TrafficLight* trafficLight) {
    std::cout << "Green Light is active." << std::endl;
}

 

#include "YellowState.h"
#include <iostream>
#include "RedState.h" 

void YellowState::EnterState(TrafficLight* trafficLight) {
    std::cout << "Traffic Light is now YELLOW. Caution!" << std::endl;
}

void YellowState::HandleTimer(TrafficLight* trafficLight) {
    trafficLight->SetState(std::make_unique<RedState>());
}

void YellowState::Update(TrafficLight* trafficLight) {
    std::cout << "Yellow Light is active." << std::endl;
}

 

그리고 다음을 활용해서 main에서 관리자인 TrafficLight를 통해 상태를 동적으로 관리해주겠습니다.

 

#include "TrafficLight.h"
#include <iostream>

int main() {
    TrafficLight light;

    for (int i = 0; i < 6; ++i) {
        std::cout << "\nTimer event triggered:" << std::endl;
        light.HandleTimer();
        light.Update();
    }

    return 0;
}

 

 

틀린 내용이 있다면 알려주시면 감사하겠습니다 !

 

+ Recent posts