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.
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().
musicforthemasses wrote: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).
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.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: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 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 questionAgain, 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 scoreSee 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.