This discussion is archived
5 Replies Latest reply: Sep 18, 2010 3:18 AM by 843853 RSS

Design pattern for quiz module

843853 Newbie
Currently Being Moderated
Hello fellow developers.

I am making/refactoring a module in a jee mobileplatform. It's a quiz module where the enduser participates via sms. Now, I am trying to redesign the module and I am thinking about using the state pattern for this, where the user can be in i.e the following states: StartQuizState(welcomemessage and first question), NextQuestionState, NoAnswerState(the user sent an empty or no valid response), QuizFinishedState(sum up the points etc), and maybe a BonusState if the user makes x points within a specific time limit.

I was wondering if anyone had any comments on my choice of pattern for this module :)

Thanks in advance for any comments,

Best regards,

Eivind
  • 1. Re: Design pattern for quiz module
    jduprez Pro
    Currently Being Moderated
    I was wondering if anyone had any comments on my choice of pattern for this module :)
    Based on the limited description of your functionalitites and of your design, I'd say it doesn't look like a good idea.
    The State pattern is not meant to implement a finite state machine as such (in particular, the transitions are not necessarily implemented in the XxxState classes themselves), it's meant to encapsulate state-dependant logic, or, awkwardly but more accurately, state-dependant implementation of operations that are defined for all states.
    There was a recent topic about this in "Java Programming", see here.
    I am thinking about using the state pattern for this, where the user can be in i.e the following states: StartQuizState(welcomemessage and first question), NextQuestionState, NoAnswerState(the user sent an empty or no valid response), QuizFinishedState(sum up the points etc), and maybe a BonusState if the user makes x points within a specific time limit.
    Across those states, I don't see which operation would be common to all, except maybe a getMessage() or hasMoreQuestions().
    If there is no operation specified in the abstract State superclass, then the State pattern has no interest here.
  • 2. Re: Design pattern for quiz module
    843853 Newbie
    Currently Being Moderated
    Thank you very much for the feedback, I will read a little bit more before I make a desition, and then post it here again so others can gain from my experiences as well :)

    The reason I chose the state pattern was that I didn't want to end up with one big class with a lot of ifs and elses.

    When a user enters, I have to check:
    - if he is already participating in a quiz, if he is, check if its the last question, then finish, or give him one more question
    - if he is not participating yet then give him a welcome message and the first question
    - what questions he has already answered and keep score

    So I check his "condition" when he enters, then sets his state, then I call process on the currentState, which handles whatever logic that corresponds with his state.

    In the abstrac class I have methods that are common, like fill in the points in the sms to be sent, and the actual sending of the sms.

    I will check some other patterns to see what fits my needs, feel free to interrupt if you have more comments :)

    E
  • 3. Re: Design pattern for quiz module
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    musicforthemasses wrote:
    The reason I chose the state pattern was that I didn't want to end up with one big class with a lot of ifs and elses.
    What makes you think that implementing user choices is going to create a "big" class?
  • 4. Re: Design pattern for quiz module
    800387 Newbie
    Currently Being Moderated
    musicforthemasses wrote:
    Thank you very much for the feedback, I will read a little bit more before I make a desition, and then post it here again so others can gain from my experiences as well :)
    A great attitude to have in these forums and programming in general. Commendable. (BTW, that is the most creative spelling of 'decision' that I have ever seen).
    The reason I chose the state pattern was that I didn't want to end up with one big class with a lot of ifs and elses.
    One normally uses polymorphism to eliminate the if's and else's. Here is a good introduction. Note that this is not a pattern per se but simple use of one of the precepts of OO.
    When a user enters, I have to check:
    - if he is already participating in a quiz, if he is, check if its the last question, then finish, or give him one more question
    Why? I could easily conceive of a user having multiple simultaneous games. You really want to track a game. The game has some array or collection or questions, and likely a variable storing which one is active. Each round could consist of a question and should also have something to track whether the answer received was correct or incorrect.
    - if he is not participating yet then give him a welcome message and the first question
    Again, if your game class has an indicator position of zero (as an example, since this works for both collections and arrays), then you know it has just started.
    - what questions he has already answered and keep score
    See above.
    So I check his "condition" when he enters, then sets his state, then I call process on the currentState, which handles whatever logic that corresponds with his state.
    Again, it's the state of the game, not the user.
    In the abstrac class I have methods that are common, like fill in the points in the sms to be sent, and the actual sending of the sms.
    The first article I linked you to will hopefully demonstrate that delegation is better than inheritance in most situations.
    I will check some other patterns to see what fits my needs, feel free to interrupt if you have more comments :)
    Don't look for patterns. That's totally backwards. Look for solutions. Patterns are about communication.
    E
    - Saish
  • 5. Re: Design pattern for quiz module
    843853 Newbie
    Currently Being Moderated
    Good morning Saish, sorry for my late response.

    First of all - thanks again for showing such interest in my "discussion", and my deepest apologies for my grammatical error hehe, I was tired and wrote a little bit too fast. Normally I take great pride in writing correctly :)

    I would also like to say that many, many times before, these forums, and others, have helped me when stuck in a programming problem or other problems related to software. So without all those people out there willing to spend their free time answering questions from people like me, a lot of us wouldn't be able to solve whatever problem we might have, and basically the general experience amongst inexperienced programmers would evolve much slower. So Saish, thanks to people like you there are a lot of happy programmers out there. It is very important for those of us working with development, to share whatever knowledge we might have.

    Anyway back to the discussion:)

    I totally agree, the state is absolutely the Quiz, not the contestant of the quiz. I just read a Head first design patterns (a great book by the way!), and I was a bit to hung up on using patterns instead of looking for a solution that works. So what I have done so far, refactoring my solution:

    I have the following models:
    - Quiz
    - Contestant
    - Answer
    - QuizManager

    Quiz Object
    The Quiz model that represents the actual quiz, which contains a list of Question objects, and Answer objects. The quiz has a int constant QUIZ_STATE, and the possible states:
    private static final int START_QUIZ_STATE = 0;
    private static final int FINISH_QUIZ_STATE = 1;
    private static final int NEXT_QUESTION_STATE = 2;
    private static final int REPEAT_QUESTION_STATE = 3;
    private int quizState = START_QUIZ_STATE;

    As you can see the quizState defaults to START_QUIZ_STATE. The first thing I do when the contestant enters, is to check the state of the quiz, if he already is an contestant, if the quiz is finished etc.

    Then I continue and process the quiz based on the state of the quiz (this time everything in one class - a quizmanager).

    Contestant Object
    Contestant model containing what questions are already answered, number of points and so on.

    Answer Object
    The Answer object is created if the contestant sends an answer and his answer corresponds with the alternatives in the Answer object. E.g if the answer alternatives are a) 100 kilometers and b) 20 kilometers, and the contestant sends an answer c, it is considered as no answer, and the last question is repeated, if the contestant sends a, or b, his answered is compared with the current Answer alternative, and a new question is sent him.

    I have some database tables for managing this module, and basically a Quiz is configured as an XML, where 1 xml is a quiz, and 1 quiz xml can contain many question and answer tags. These are read upon quiz start, and Quiz, Answer, and Question objects are created. Every time a new contestant enters, a new Contestant object is ofcoursed created.

    All the logic takes place in the previously mentioned QuizManager. Any comments on the solution so far would be greatly appreciated.

    If interesting to anyone I can post the classes when I am finished with the module, as a standalone java application without a database.

    Regards,

    Eivind