0

I'm wondering about a good way to deal with a Question/Answer - Model for multiple Users. My goal is to have an efficient and readable model where all the results of my users are stored. Every user has the same questions/answers!

At the moment I have three classes, Question,Answer and User.

A user has a Set of Questions. A question has a Set of Answers and an extra field selected_value which is the answer the user clicked.

Every user has a copy of the same set of questions. Therefore I can easily see what a user has clicked.

This is an unnecessary overload because I need to store the questions/answers more than once.

But the good thing about this approach is the availability of my questions/answers in my user-model as I need to see what every user clicked. It's also easy to extract this information (e.g. via xml) doing just a loop around every user and his questions.

But what kind of approach would you recommend to use? Should I create a model "result" inside of my User-Model where I simply store a list of pairs like "QuestionId, AnswerId"? Is there any more accepted way to do so?

  • User-Model - are you using an ORM? Which one? – Izkata Mar 30 '14 at 2:07
  • 1
    Is there a database? Are the users concurrent? Is this a web app? – david.pfx Mar 30 '14 at 3:09
  • This is just an abstraction - let's say I just have these three parts of an application. It doesn't matter if I'm using an ORM. I'm programming in an object-oriented language :) – Frame91 Mar 30 '14 at 14:50
1

A User may take many Sets of Questions, at different times or even more than once - I think you need a new class here to store:

(a) The association of a User with a Set of Questions (Quiz?), possibly at a particular time, and

(b) The answers selected by that User for that particular Set of Questions.

I would consider a new class, created based on the data within a Set of Answers and containing a new field called, say, Chosen_Answer. In this way, the Sets of Answers class itself stays clean, self-contained and, most importantly, exists independently of any particular User, so you don't need duplicated copies any more. When a User takes a Set of Questions, you create a new instance of this new class in which to hold their responses.

1

Since it looks like the questions and the set of possible answers are the same for all users, I would say that it is fine if you share the same data with all the users. You can use composition for this.

The only piece of data that varies according to your descriptions is the user's selected answer for every question. As such, a user may have another collection data structure to store the selected answer per questions, somewhat like

class User{
  private final List<Question>; 
  private final Map<Question, Answer> answers;

  public User(List<Question> questions){
      //shared by all users
      this.questions = questions;
      //an instance per user
      this.answers = new HashMap<>();
  }
}

When it comes to storage, the map of answers will probably be just a table (or document, file, store,etc.) containing the IDs of the questions and the ID of selected answer for a given user.

+-----------------+
| selected_answer |
+-----------------+
| user_id  <pk>   |
| quest_id <pk>   |
| ans_id   <pk>   |
+-----------------+

Doing so, avoids repetition in both memory and storage.

0

When you design a DB you may want to keep it normalized I.e. stay out of duplicates so that update queries will run faster and in a safer way. This is not the only approach as you might want to de-normalize your tables so that read queries will run faster (more suitable for BI). The point is that there's no right or wrong- both are fine depending on your needs.

Regarding your case- if the quantities we are talking about here are not large, and performance is not an issue, I would go with storing only a representation of the selected answer in your user object (maybe an id or an answer object). It will make things easier and simpler. Things you should consider are CRUD operations and their frequency- how frequently do you change the text of an answer? A question? How frequently does the selected answer changes if ever? And so on..

One more thing- keeping your domain objects design synchronized with the actual db design is not a must so you may have one normalized and the other de-normalized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.