我把我的想法整理了一下写了一个演示:
头文件:
#ifndef _MODE_1_H
#define _MODE_1_H
#include <iostream>
using namespace std;
// Page ID
enum PAGEID
{
PAGE_MAIN = 0, // Page Main
PAGE_FREEMAN, // Page Freeman
PAGE_XIANREN, // Page Xianren
PAGE_EXIT, // Exit
PAGE_NUMBER // Page number
};
class Base;
class PageMain;
class PageFreeman;
class PageXianren;
// base class
class Base
{
public:
virtual PAGEID DoSomething() = 0;
protected:
// Change to other page which page ID is num
PAGEID ChangeTo();
};
// Welcome page
class PageMain:public Base
{
public:
PAGEID DoSomething();
};
// Freeman's page
class PageFreeman:public Base
{
public:
PAGEID DoSomething();
};
// Xianren's page
class PageXianren:public Base
{
public:
PAGEID DoSomething();
};
// Main process class, singleton
class MainProc
{
public:
// Get Instance
static MainProc* Instance();
// Execute system
void Go();
protected:
MainProc():m_pBase(NULL){}
virtual ~MainProc(){}
// Do something
PAGEID DoSomething();
// Get new page
Base *GetNewPage();
// Instance of MainProc
static MainProc *m_pInstance;
PAGEID m_pageid;
// Current page pointer
Base *m_pBase;
};
#endif
#define _MODE_1_H
#include <iostream>
using namespace std;
// Page ID
enum PAGEID
{
PAGE_MAIN = 0, // Page Main
PAGE_FREEMAN, // Page Freeman
PAGE_XIANREN, // Page Xianren
PAGE_EXIT, // Exit
PAGE_NUMBER // Page number
};
class Base;
class PageMain;
class PageFreeman;
class PageXianren;
// base class
class Base
{
public:
virtual PAGEID DoSomething() = 0;
protected:
// Change to other page which page ID is num
PAGEID ChangeTo();
};
// Welcome page
class PageMain:public Base
{
public:
PAGEID DoSomething();
};
// Freeman's page
class PageFreeman:public Base
{
public:
PAGEID DoSomething();
};
// Xianren's page
class PageXianren:public Base
{
public:
PAGEID DoSomething();
};
// Main process class, singleton
class MainProc
{
public:
// Get Instance
static MainProc* Instance();
// Execute system
void Go();
protected:
MainProc():m_pBase(NULL){}
virtual ~MainProc(){}
// Do something
PAGEID DoSomething();
// Get new page
Base *GetNewPage();
// Instance of MainProc
static MainProc *m_pInstance;
PAGEID m_pageid;
// Current page pointer
Base *m_pBase;
};
#endif
cpp文件:
#include <iostream>
#include "mode1.h"
using namespace std;
/* Class Base */
// Get next page id
PAGEID Base::ChangeTo()
{
cout<<"mission complete, please select next page:"<<endl;
size_t pageid;
cin >> pageid;
return pageid;
}
/* Class PageMain */
PAGEID PageMain::DoSomething()
{
cout<<"Main is doing something"<<endl;
return Base::ChangeTo();
}
/* Class PageFreeman */
PAGEID PageFreeman::DoSomething()
{
cout<<"Freeman is doing something"<<endl;
return Base::ChangeTo();
}
/* Class PageXianren */
PAGEID PageXianren::DoSomething()
{
cout<<"Xianren is doing something"<<endl;
return Base::ChangeTo();
}
/* Class MainProc */
MainProc *MainProc::m_pInstance = NULL;
// Get Instance
MainProc * MainProc::Instance()
{
if (NULL == m_pInstance)
{
m_pInstance = new MainProc();
}
return m_pInstance;
}
// System process
void MainProc::Go()
{
m_pBase = new PageMain();
while (NULL != m_pBase)
{
m_pageid = DoSomething();
delete m_pBase; // delete old page
m_pBase = GetNewPage(); // change to the new page
}
}
PAGEID MainProc::DoSomething()
{
return m_pBase->DoSomething();
}
Base *MainProc::GetNewPage()
{
switch (m_pageid)
{
case PAGE_MAIN:
return new PageMain();
case PAGE_FREEMAN:
return new PageFreeman();
case PAGE_XIANREN:
return new PageXianren();
case PAGE_EXIT:
cout<<"End of system, Exit"<<endl;
return NULL;
default:
cout<<"Invalid Page ID, Exit"<<endl;
return NULL;
}
}
int main()
{
MainProc::Instance()->Go();
system("pause");
return 0;
}
#include "mode1.h"
using namespace std;
/* Class Base */
// Get next page id
PAGEID Base::ChangeTo()
{
cout<<"mission complete, please select next page:"<<endl;
size_t pageid;
cin >> pageid;
return pageid;
}
/* Class PageMain */
PAGEID PageMain::DoSomething()
{
cout<<"Main is doing something"<<endl;
return Base::ChangeTo();
}
/* Class PageFreeman */
PAGEID PageFreeman::DoSomething()
{
cout<<"Freeman is doing something"<<endl;
return Base::ChangeTo();
}
/* Class PageXianren */
PAGEID PageXianren::DoSomething()
{
cout<<"Xianren is doing something"<<endl;
return Base::ChangeTo();
}
/* Class MainProc */
MainProc *MainProc::m_pInstance = NULL;
// Get Instance
MainProc * MainProc::Instance()
{
if (NULL == m_pInstance)
{
m_pInstance = new MainProc();
}
return m_pInstance;
}
// System process
void MainProc::Go()
{
m_pBase = new PageMain();
while (NULL != m_pBase)
{
m_pageid = DoSomething();
delete m_pBase; // delete old page
m_pBase = GetNewPage(); // change to the new page
}
}
PAGEID MainProc::DoSomething()
{
return m_pBase->DoSomething();
}
Base *MainProc::GetNewPage()
{
switch (m_pageid)
{
case PAGE_MAIN:
return new PageMain();
case PAGE_FREEMAN:
return new PageFreeman();
case PAGE_XIANREN:
return new PageXianren();
case PAGE_EXIT:
cout<<"End of system, Exit"<<endl;
return NULL;
default:
cout<<"Invalid Page ID, Exit"<<endl;
return NULL;
}
}
int main()
{
MainProc::Instance()->Go();
system("pause");
return 0;
}
瞎搞的