2012-02-20 116 views
0

在我的游戏项目中,我使用的是MVC方案。模型和视图完全分离。观察由观察者模式驱动。所以当游戏实体发生某些事情时(创建,销毁,更新...),视图将通过监听器通知。当分配模型中的新实体时,我需要创建适当的视图模型(EntityMesh)。C++ MVC模型viewmodel“翻译”和继承

  • EntityMesh是基础视图模型
  • 游戏::实体是模型

这个例子模型派生树基地看起来是这样的:

  • 游戏::实体 - > Game :: PhysicalEntity - > Game :: PhysicalGridEntity
  • Game :: Entity - > Game :: ActorEntity - > Game :: FreeActorEntity

这就是我目前“解决”这个问题的方法......但是在将来我期待30个以上的Game :: Entity和EntityMesh的衍生物。这是非常丑陋的非OOP设计。

EntityMesh* WorldView::_translate(Game::Entity* const ent) 
{ 
    //TODO: finish translator... 
    std::cout << "TRANSLATING!: " << typeid(ent).name() << std::endl; 

    Game::PhysicalGridEntity* pgent = dynamic_cast<Game::PhysicalGridEntity*>(ent); 
    if(pgent) 
    { 
     std::cout << "CREATING: PhysicalGridEntity" << std::endl; 
     return new PhysicalGridEntityMesh(this, pgent); 
    } 
    else 
    { 
     Game::PhysicalEntity* pent = dynamic_cast<Game::PhysicalEntity*>(ent); 
     if(pent) 
     { 
      std::cout << "CREATING: PhysicalEntity" << std::endl; 
      return new PhysicalEntityMesh(this, pent); 
     } 
     else 
     { 
      Game::FreeActorEntity* faent = dynamic_cast<Game::FreeActorEntity*>(ent); 
      if(faent) 
      { 
       std::cout << "CREATING: FreeActorEntity" << std::endl; 
       return new FreeActorEntityMesh(this, faent); 
      } 
     } 
    } 

    return NULL; 
}; 

任何想法如何使这更清晰和更多的面向对象不混合视图和模型?

编辑:任何暗示使用什么模式将帮助:)

+1

如何使用访问者模式? – 2012-02-20 14:13:26

回答

2

首先,你没有一个控制器组件。你在哪里连接模型和视图的业务逻辑?其次,我不确定你想要做什么,但可能工厂/访客模式会帮助你。

+0

我没有提到控制器,因为它与这个问题没有任何关系。这些ifs只是一个例子,我目前正在处理这个问题。工厂/访问者模式这可能会工作... – BlackCat 2012-02-20 14:17:26

+1

我第二个BlackCat在这里,工厂/访问者似乎适当。 – qdii 2012-02-20 14:19:29