Here is a simplified requirement:
User creates a
Questionmust have at least one
Answeras in a test: there is one question, but several answers, where few may be correct. User is the actor who is preparing this test, hence he creates question and answers.
I am trying to model this simple example so to 1) match the real life model 2) to be expressive with the code, so to minimize potential misuse and errors, and to give hints to developers how to use the model.
Question is an entity, while answer is value object. Question holds answers. So far, I have these possible solutions.
[A] Factory inside
Instead of creating
Answer manually, we can call:
Answer answer = question.createAnswer() answer.setText(""); ...
That will create an answer and add it to the question. Then we can manipulate answer by setting it's properties. This way, only questions can crete an answer. Also, we prevent to have an answer without a question. Yet, we do not have control over creating answers, as that is hardcoded in the
There is also one problem with the 'language' of above code. User is the one who creates answers, not the question. Personally, I don't like we create value object and depending on developer to fill it with values - how he can be sure what is required to add?
[B] Factory inside Question, take #2
Some says we should have this kind of method in
question.addAnswer(String answer, boolean correct, int level....);
Similar to above solution, this method takes mandatory data for the answer and creates one that will be also added to the question.
Problem here is that we duplicate the constructor of the
Answer for no good reason. Also, does question really creates an answer?
[C] Constructor dependencies
Let's be free to create both objects by our selves. Let's also express the dependency right in constructor:
Question q = new Question(...); Answer a = new Answer(q, ...); // answer can't exist without a question
This gives hints to developer, as answer can't be created without a question. However, we do not see the 'language' that says that answer is 'added' to the question. On the other hand, do we really need to see it?
[D] Constructor dependency, take #2
We can do the opposite:
Answer a1 = new Answer("",...); Answer a2 = new Answer("",...); Question q = new Question("", a1, a2);
This is opposite situation of above. Here answers can exist without a question (which does not make sense), but question can not exist without answer (which make sense). Also, the 'language' here is more clear on that question will have the answers.
[E] Common way
This is what I call the common way, the first thing that ppl usually do:
Question q = new Question("",...); Answer a = new Answer("",...); q.addAnswer(a);
which is 'loose' version of above two answers, since both answer and question may exist without each other. There is no special hint that you have to bind them together.
Or should I combine C, D, E - to cover all the ways how relationship can be made, so to help developers to use whatever is best for them.
I know people may choose one of the answers above based on the 'hunch'. But I wonder if any of above variant is better then the other with a good reason for that. Also, please don't think inside the above question, I would like to squeeze here some best practices that could be applied on most cases - and if you agree, most use cases of creation some entities are similar. Also, lets be technology agnostic here, eg. I do not want to think if ORM is going to be used or not. Just want good, expressive mode.
Any wisdom on this?
Please ignore other properties of
Answer, they are not relevant for the question. I edited above text and changed the most of the constructors (where needed): now they accept any of necessary property values needed. That may be just a question string, or map of strings in different languages, statuses etc. - whatever properties are passed, they are not a focus for this ;) So just assume we are above passing necessary parameters, unless said different. Thanx!