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

  • SEARCH
  • Home
  • 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 9018637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:34:41+00:00 2026-06-16T04:34:41+00:00

Suppose I have a superclass, Model . Suppose also I have two subclasses, NumberModel

  • 0

Suppose I have a superclass, Model. Suppose also I have two subclasses, NumberModel and StringModel, one which stores a number and the other stores a string. To implement the MVC scheme, I make two views, NumberView and StringView that can edit and display the model data.

I then have a list of Models. Unfortunately, now I do not know which Models handle numbers and strings, so if I wanted to create views for all of them, I would break the object-oriented paradigm.

UNLESS…

I make a method in Model called constructView() that returns an appropriate view and override it in the subclasses. But suddenly, I’ve made the model aware of the view, and this too doesn’t feel good in the MVC scheme.

What would be the proper way to implement this from a design perspective?

  • 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-06-16T04:34:42+00:00Added an answer on June 16, 2026 at 4:34 am

    Maybe the visitor pattern/double dispatch is the way to go? It gets a bit convoluted, and is perhaps overly complex if you only have two models/views. It’s perhaps not the most elegant solution, but here goes:

    /* Adding a new model involves adding a new method to this base class. Not very 
     * elegant but that's the way the pattern works...  */
    public class View {
        public void render(AModel model) { /* implement in subclass */ }    
        public void render(BModel model) { /* implement in subclass */ }
    }
    
    /* Providers provides the views with models, and serves to decouple
     * models from views */
    public interface ModelProvider { 
        public void renderWith(View view); 
    }
    
    public class AModelProvider implements ModelProvider {
        private final AModel _model;
        public void renderWith(View  view) { view.render(_model); }
    }
    
    public class BModelProvider implements ModelProvider { 
        private final BModel _model;
        public void renderWith(View  view) { view.render(_model); }
    }
    
    public class AView extends View { 
        public void render(AModel model) { 
            model.doA(); 
        } 
    }
    
    public class BView extends View { 
        public void render(BModel model) { 
            model.doB(); 
        } 
    }
    
    public class Controller {
        public void renderModels(List<ModelProvider> providers, View view) {
            for (ModelProvider provider : providers) {
                // if provider is an AModelProvider and view is an AView,
                // render(AModel) will be called. If provider is a BModelProvider
                // or if view is BView, nothing will happen since the empty
                // View base class methods will be called...
                provider.renderWith(view);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a common superclass (suppose it's Employee ), which has (amongst others) subclasses
Suppose I have two tables, - emp(empId number(1),empName varchar2(50)) and - manager(manId number(5),managerName varchar2(100))
Suppose you have val docs = List(List(one, two), List(two, three)) where e.g. List(one, two)
Suppose I have a superclass which imports java.io.Exception as an example. If I'm handling
Suppose I have a superclass Entity and some subclasses Creature , Heroes etc. I
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose I have two h:inputText components. I want to bind both of the text
Suppose I have MyEdge and MyEdgeModified , such that MyEdge is the superclass and
Suppose I have an abstract class Person . There is another class Student which
Lets suppose i have a super class WorkStation , and two subclass StationNormal ,

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.