C++设计模式-外观模式,游戏引擎管理多个子系统,反汇编
// 物理引擎子系统
class PhysicsSystem
{
public:
void init() { cout << "Initializing physics engine..." << endl;}
void update() { cout << "Updating physics..." << endl; }
};
//渲染子系统
class GraphicsSystem
{
public:
void init() { cout << "Initializing graphics..." <<endl; }
void render() { cout << "Rendering graphics..." << endl;}
};
//粒子子系统
class ParticleSystem
{
public:
void init() { cout << "Initializing particle..." << endl; }
void createParticle() { cout << "Creating particle..." << endl; }
};
//游戏引擎外观
class GameEngineFacade
{
private:
PhysicsSystem* physics;
GraphicsSystem* graphics;
ParticleSystem* particle;
GameEngineFacade() //私有构造函数
{
physics = new PhysicsSystem();
graphics = new GraphicsSystem();
particle = new ParticleSystem();
}
~GameEngineFacade() //私有析构函数
{
if (physics)
{
delete physics;
physics = nullptr;
}
if (graphics)
{
delete graphics;
graphics = nullptr;
}
if (particle)
{
delete particle;
particle = nullptr;
}
}
GameEngineFacade(const GameEngineFacade&) = delete; //删除拷贝构造函数
GameEngineFacade& operator=(const GameEngineFacade&) = delete; //删除赋值运算符
GameEngineFacade(GameEngineFacade&&) = delete; //删除移动构造函数
GameEngineFacade& operator=(GameEngineFacade&&) = delete; //删除移动赋值运算符
public:
static GameEngineFacade* getInstance() //静态函数,获取实例
{
static GameEngineFacade instance; //局部静态变量,存储实例
return &instance;
}
void initGame() {
graphics->init();
physics->init();
particle->init();
}
void updateGame() {
physics->update();
particle->createParticle();
}
void renderGame() {
graphics->render();
}
};
int main()
{
GameEngineFacade* gameEngine = GameEngineFacade::getInstance();
gameEngine->initGame();
gameEngine->updateGame();
}