본문 바로가기
C++/0x01-design pattern

state 패턴

by SpeeDr00t 2016. 7. 14.
반응형

state 패턴

1.소스

#include <stdio.h>
#include <iostream>

using namespace std;


class YState
{
public:
    YState();
    virtual ~YState();
 
    virtual void init() = 0;
    virtual void Draw() = 0;
    virtual void ReleaseAll() = 0;
};


YState::YState()
{

}

YState::~YState()
{

}

 
class YLogo : public YState
{
public:
    YLogo();
    ~YLogo();
    //virtual

    void init();
    void Draw();
    void ReleaseAll();
};


YLogo::YLogo()
{
}

YLogo::~YLogo()
{
}

void YLogo::init()
{
    printf("YLogo Init\n");
}

void YLogo::Draw()
{
    printf("YLogo Draw\n");
}

void YLogo::ReleaseAll()
{
    printf("YLogo ReleaseAll\n");
}
 

class YGameMain : public YState
{
public:
    YGameMain ();
    ~YGameMain ();
    //virtual

    void init();
    void Draw();
    void ReleaseAll();
};

YGameMain::YGameMain()
{
}

YGameMain::~YGameMain()
{
}

void YGameMain::init()
{
    printf("YGameMain Init\n");
}

void YGameMain::Draw()
{
    printf("YGameMain Draw\n");
}

void YGameMain::ReleaseAll()
{
    printf("YGameMain ReleaseAll\n");
}
 
 
 
class GameMgr
{
public:
    GameMgr();
    ~GameMgr();
	
   void ChaneState(YState* pState);
   YState* GetState(){return m_pState;};
   
private:
    YState* m_pState;
};

GameMgr::GameMgr()
{
    m_pState = NULL;
}

GameMgr::~GameMgr()
{
}

void GameMgr::ChaneState(YState* pState)
{
    m_pState = NULL;
    m_pState = pState;
}
 
 
 
int main( int argc , char ** argv )
{
    GameMgr* pGameMgr = new GameMgr;
 
    pGameMgr->ChaneState(new YLogo);
    pGameMgr->GetState()->init();
    pGameMgr->GetState()->Draw();
    pGameMgr->GetState()->ReleaseAll();

    pGameMgr->ChaneState(new YGameMain);
    pGameMgr->GetState()->init();
    pGameMgr->GetState()->Draw();
    pGameMgr->GetState()->ReleaseAll();
 
  return 0;
}

2.결과

hacker@HACKER:~/cpp$ g++ -o state_pattern state_pattern.cpp

hacker@HACKER:~/cpp$ ./state_pattern
YLogo Init
YLogo Draw
YLogo ReleaseAll
YGameMain Init
YGameMain Draw
YGameMain ReleaseAll
반응형

'C++ > 0x01-design pattern' 카테고리의 다른 글

decorator pattern.  (0) 2016.07.14
composite pattern  (0) 2016.07.14
Observer pattern  (0) 2016.07.14
design pattern ][ Abstract Factory pattern  (0) 2016.07.14
design pattern ][ SingleTon  (0) 2016.07.14