Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7643271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:24:24+00:00 2026-05-31T09:24:24+00:00

I have a model view architecture. I want to create a view object from

  • 0

I have a model view architecture. I want to create a view object from a model object.
Here is a basic example.

How do i specify the type of my model objects: enums (but then they are not extendable), ints (but then easy to get messed up) …

class ModelA
{
    ModelA(int a);
}

class ModelB : public ModelA
{
    ModelB(int a,int b);
}

class ViewModelA
{
    ViewModelA(ModelA* ma);
}

class ViewModelB : public ViewModelA
{
    ViewModelB(ModelB* mb);
}

class ViewFactory
{
     ModelA * createFromModel(ModelA *ma)
     {
          // Now what is the best way to store type ???
          // Used an integer and overload a static method getType()
          // Or use an enum?
          switch(ma.type)
          {

          }
     }
}


int main(int argc, char *argv[])
{
    ModelA *ma = new ModelA(10);
    ModelA *mb = new ModelB(10,11); 
    ViewFactory myFactory;
    ViewModelA *va = myFactory.createFromModel(ma);
    ViewModelB *vb = myFactory.createFromModel(mb);
    //va should be a model of ViewModelA and vb a model of ViewModelB
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:24:26+00:00Added an answer on May 31, 2026 at 9:24 am

    IMHO, from your question and comment on other answer, you can also use Visitor pattern here.
    Read about it here or here

    So, the model hierarchy would be visited by the Visitor.
    The responsibility of the Visitor will be to create appropriate View object for the visited Model type.

    Then you do not need to store/declare model type.

    Working code: Also live here: http://ideone.com/9A5bJ

    #include <iostream>
    
    
    
    class Visitor{
    public:
        virtual ~Visitor(){}
        virtual void visitModelA(class ModelA*) = 0;
        virtual void visitModelB(class ModelB*) = 0;
        //add visit functions for later defined models
    
    };
    
    
    
    
    
    class View{
    public:
        virtual ~View(){}
    };
    
    class ViewA : public View{
    public:
        ViewA(){
            std::cout << "Created ViewA\n"; 
        }
    };
    
    
    class ViewB : public View{
    public:
        ViewB(){
            std::cout << "Created ViewB\n"; 
        }
    };
    
    
    
    
    
    
    
    
    class Model{
    public:
        virtual ~Model(){}
    
        virtual void Accept(Visitor* visitor) = 0;
    
    };
    
    class ModelA:public Model{
    public:
    
        virtual void Accept(Visitor* visitor){
            visitor->visitModelA(this);
        }
    };
    
    class ModelB:public Model{
    public:
        virtual void Accept(Visitor* visitor){
            visitor->visitModelB(this);
        }
    };
    
    
    
    
    
    
    // ViewCreator can be made a private class of ViewFactory
    class ViewCreator : public Visitor{
    public:
        virtual void visitModelA(class ModelA*) {
            _view = new ViewA();
        }
        virtual void visitModelB(class ModelB*) {
            _view = new ViewB();
        }
    
        //add visit functions for later defined models
    
        View* GetView(){
            return _view;
        }   
    
    private:
        View* _view;
    };
    
    
    class ViewFactory{
    public:
        static View* CreateViewFor( Model* model){
            ViewCreator viewCreator;
            model->Accept(&viewCreator);
            return viewCreator.GetView();
        }
    
    };
    
    
    int main(){
    
        ModelA modelA;
        View* viewA = ViewFactory::CreateViewFor(&modelA);
    
    
        ModelB modelB;
        View* viewB = ViewFactory::CreateViewFor(&modelB);
    
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MVVM architecture. I have a usercontrol UC as a View Model
Both OOD (Object-Oriented-Design) and MVC (Model-View-Controller) architectures have become staples of modern software design.
A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.
I have a view model with a property Fields which is an ObservableCollection<FieldVM> .
In my view model if have a: List<Car> where car has an Id and
if i have created a view model and have a partial form that is
I have a Java project that I'm trying to implement with a model-view-controller design.
I am newbie at Django. I have model with a custom method. In view
I have a model, view and controller not interacting correctly, and I do not
How do I bind a view model property to the ListBox.SelectedItem property? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.