views:

85

answers:

1

I was given a Use Case for a Quizz Application. The Use Case is only for creating new Quizzes. I am having trouble deciding which design is better:

a)

alt text

b)

alt text

Although this might look like a Domain Model, it is in fact a Class Diagram (I was lazy to put the methods/attribtues in the diagrams :( ).

The idea behind all this is that I have a QuizCatalog that has Quizzes. Each Quiz has a set of Questions, that must be created through a QuestionFactory(a Question is an abstract class, and QuestionA, QuestionB, etc are the concrete classes). Each Question has a set of PossibleAnswers.

The difference in terms of associations between Design A and Design B is that in the first I am considereing that CreateQuizController will simply delegate every task it has to QuizCatalog. If QuizCatalog needs to do something, it will delegate everything it might need down the hierarchy. This is actually nice, as it seems to reduce coupling.

Design B, on the other hand, follows a different philosophy. The associations seen in Design A still exist (as a QuizCatalog still has Quizzes, a Question PossibleAnswers, etc) but now I've made CreateQuizController have access basically to every kind of object in the domain it might need to create in the process(I've signaled those kind of associations with d). The idea is that instead of asking the QuizCatalog to create a Quiz, CreateQuizController will create a Quiz by itself (and if it needs to create Questions for the Quiz, it will by itself, the same happening for Question's PossibleAnwsers, etc).

There are 2 things that bother me about Design A:

1.

If I need to create temporary objects that need to be filled up before being put "in the system" (for example, a Quiz is only actually added to QuizCatalog after it was filled with all the wanted Questions), following this design I'll have to keep them in some place other than the Controller.

For example, when I first create a Quiz, I'll have to probably save it under QuizCatalog without actually adding it to the current collection of Quizzes that is accessible to the rest of the system. I find this kind of behaviour a bit awkward. I find it better to keep those kind of temporary objects in a Controller, as if anything wrong happens then the "System" is kept just as it was before, with no problems associated.

The problem is that it makes the Controller have to know about almost everything, which might be undesirable. On the other hand, there is no difference in how coupled the rest of the classes in the System are.

2.

If I am going to use Design A, I actually don't see a big point in having a CreateQuizController, as basically everything can be done by just having a reference to QuizCatalog. In my eyes, it is just delegating all its work to QuizCatalog, so why have it in the first place?

Also, using Design A, I'd probably consider QuestionFactory to be a Singleton, while if using Design B I'd probably just have CreateQuizController's constructor accept an instance of a QuestionFactory instead.


What are your thoughts about this?

Thanks

PS: Only after drawing the diagrams I noticed that Quizz has 2 z's :( My bad.

+1  A: 

Just from the naming I think, the CreateQuizController has too many responsibilities. I assume (again from its name), that it can create quizzes and control quizzes. What about separating this in two classes, a QuizFactory and a QuizController?

So if we need a new quiz, we ask the QuizFactory to create a new one, the QuizFactory would then create a new quiz, use the QuestionFactory to create questions and possible answers and we would add the new quiz to the (static) QuizCatalog.

If we want to run a quiz, we then would select a quiz from the catalog, create a QuizController (with this quiz), and the QuizController would start the quiz, present the questions and keep the score.

The QuizController wouldn't need to know the questions, it would be enough if the quiz offered methods like getNextQuestion() and getPreviousQuestion() and a method to validate the candidate's answer (so the controller would know the actual question only).

So a has-a relation from QuizController to Quiz would be enough. And in my design, the QuizCatalog would be (like) a singleton and thus static or owned by another top-level class, like some QuizManager.

Andreas_D
Actually the idea was that the CreateQuizController would only create quizes (it is not supposed to manage them later). The name came from the fact that it is a Controller (in the sense that it is "upper") and not from the fact that it will later manage quizzes.
devoured elysium
So my question is, how should I make (your) QuizFactory work? Should it know all the objects in the domain and create them, or should it just delegate that kind of work to the Quiz Catalog? Edit: I see you prefer the first one (through the factory). Hmm
devoured elysium
Now that I think of it, I think that what we actually have here is a kind of Builder Pattern, because I to create the Quiz, I have to also create all the questions, and for each one of the questions, all the possible answers. Do I make sense?
devoured elysium
Sounds pomising - just looked the the pizza-example at wikipedia (http://en.wikipedia.org/wiki/Builder_pattern#Java) and it could be a match or at least a basis for the *quiz creation* part in your design. Introducing a QuizBuilder is worth a try!
Andreas_D
Other question, what should be the job of a Catalog? Just a get for the set of questions it has "inside it"? Or should it also handle adding and removing elements, etc? From what I've seen you say, it seems it definitely should NOT create them, right?
devoured elysium
I'd just implement a sort of registry for quizzes, just basic get, add and remove operations. Maybe some book keeping (last time used, used how many times, avererage score, ...). But creating a quiz is not the registries responsibility. At most it could *own* a (set of) quiz factory(ies).
Andreas_D
i thought the builder pattern is for complex objects with many (optional) properties. andreas is right, the only thing im missing is a quizfactory, which fills the quizcatalog.
atamanroman
The thing is that in the Use Case, you have a set of steps before actually building the whole quiz. So in that case it does make sense to use the Builder Pattern, imo. But you couldn't know it, as I didn't mention it in the OP.
devoured elysium
cant you just build an options object during these steps and hand it over to your quizfactory? you wont have quiz objects in undefined states then. I see it like this: builder are for complicated objects with many fields (which leads to long and many constructors due to optional elements or undefined states between initialization by setters) while factorys are for object creation which needs multiple steps from different sources. so which fits best?
atamanroman