This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
as you can see a student has midterm, extracurricular and attendance. midterm has its own calculateGrade() and student also has calculateGrade(). A student's grade is the sum of his/her midterm calculateGrade and calculateGrade in the student class itself. among student's members, only midterm has points (calculateGrade) while others don't. I'm tempted to apply decorator pattern, but I don't want to involve inheritance. any other pattern? thanks
Rather than trying to find a pattern to use, why don't you evaluate your design and understand what kind of problems you're having? I would start with SOLID design principles first and see what principles of good OO design have been violated by this code. Once you have that understanding, you may find it easier to match the problems you're trying to solve with the problems that certain patterns are trying to address.
You mentioned the Decorator pattern. Does your code have the same problem that the Decorator pattern tries to address? Why do you hesitate to use inheritance? What kind of problem does that create in your code?
I'm avoiding inheritance because the objects are parsed from XML using JAXB. I don't know the effect of inheritance to parsing process. Also I've opened tha SOLID page you gave, but I still don't know what principle violated by this code. Could please give me a hint?
One problem I can immediately see in this code is the lack of conceptual cohesiveness. That is, the ideas you are trying to model in the code don't come together very well. For example, you have a calculateGrade method in a Student and a calculateGrade method in a Midterm. There's no symmetry there because a Student is a fundamentally different concept from a Midterm exam. However, if you were to say that Midterm, PopQuiz and Final are all types of Exam that you could ask for a grade by calling a calculateGrade method on them, then there is some sense in that. I can relate to those ideas and the story that your code would tell would be much clearer.
Now, whose responsibility is it to calculate a grade? Is it the Student who should be responsible? It doesn't seem right.
Also, be aware that it is sometimes counterproductive to model all objects after their counterparts in the real world. That is, while a student in the real world can certainly calculate their own grades, this idea doesn't necessarily translate very well into a good object-oriented design. Rather, you need to look at the data and the actions that need to be performed on that data. The actions and data combine to become objects. Good design seeks to bind information and actions performed on them very tightly. Bad designs arise when that tight binding (encapsulation) is violated or when there is no tight binding between information and actions at all.
So, if not Student, then who should be responsible for calculating grades? Remember, a good OO design doesn't necessarily have to mimic the real world... I would not recommend a Teacher class here.
Hendra Kurniawan wrote:I'm avoiding inheritance because the objects are parsed from XML using JAXB. I don't know the effect of inheritance to parsing process.
I'm sorry, but I don't even know how to start making the connection between inheritance and parsing XML using JAXB... Can you elaborate a little more so I can see where you are going / coming from with this line of thinking?
Joined: Jan 31, 2011
for inheritance, say student object inherits person object. Now I haven't played with XML before and barely managed to do POJO <-> XML. I don't know what will happen if this:
turns to this
So, I'm just trying to keep things simple.
Second, about why I put grade in student and midterm, it's because I'm following example about coffee making if I'm not mistaken in the design pattern book. It seems so similar except the example used inheritance. So how do you suggest grading the student? and yes, I thought of moving the responsibility to Teacher class. Do you have better approach?
so does attendance go into the grade? does extracurricular go into the grade? activity name? teacher name?
If your problem is calculating a grade, why do you have all this stuff? What is the exact information that goes into a grade? Ok, I admit, that's a rhetorical question. So it's just points, right? So again, why all this other stuff?
Joined: Jan 31, 2011
the only ones with points : midterm and attendance. Extracurricular is just additional information to be displayed. As for name, well, it's quite obvious why it's needed. Extracurricular also serves as a "different" member so I can see how decorator solves problem where some member doesn't have grading ability.
Hendra Kurniawan wrote:Extracurricular also serves as a "different" member so I can see how decorator solves problem where some member doesn't have grading ability.
Again, I am struggling to make a connection here. What does a Decorator have to do with extracurricular not having a grading ability?
Joined: Jan 31, 2011
nothing, that's the point. I want to know if decorator that I suggested in the first place can handle a case where not all of the decorated object's member have grading ability. but then you said decorator pattern doesn't suit this business logic, and so I wonder what other approach suitable for this requirement.
Are you talking about the requirement to calculate a grade based on midterm + attendance? Why are you so bent on finding a pattern? There's no rule that says you have to use a pattern. Patterns are nice to have if your problem is similar to the problem that the pattern addresses. Otherwise, looking for a pattern to solve a problem becomes more of a distraction. Forget about patterns for now.
Answer this simple question: How do you calculate a grade? Just one single student's grade -- how do you calculate it?
Joined: Jan 31, 2011
point=(attendance + grade)/2:
attendance = attendance days/effective teaching days
grade : grade/4 --> grade are whole number between 0 - 4 inclusive
Ok, seems we're getting somewhere now. Some things still not clear with these formulas:
grade : grade / 4 ; where grade is a whole number from 0 to 4.
So grade (left side) can be any of 0/4, 1/4, 2/4, 3/4, 4/4? If this is correct, you need to be clear about what the left side "grade" is vs. the right side "grade" -- using "grade" to refer to both the left-hand and right-hand side is ambiguous and confusing
Also, I didn't see where you had "effective teaching days" in the first post. So let me try to remove some ambiguities and you tell me if I got it right:
grade_points = grade / 4 where grade has a value from 0 to 4 which is presumably the "score" they got for their Midterm exam.
Hendra, it's way past my bedtime now. Was just up waiting for my computer to finish a long-running task but I need to get some rest. I'll leave you with this: If I got the formulas right, then I don't see any need WHATSOEVER for a pattern here. A single class that contains the information you have to use to calculate a final grade is all that's really needed here. Nothing much fancier than that. Think about it: Where does a teacher keep all the information about students' scores and attendance? Back in my day, this was done manually in a class record book. It looks like a ledger, where each row is for one student and each column after the student's name is some component that goes towards the student's final grade.
As I see it, the only object you really need to do all the work for the requirement of "Calculate a grade for a Student" is something that corresponds to a single row in that "Class Ledger". Like I said before, a good OO design doesn't really have to use patterns and it doesn't have to be modeled just like the real world.
Think of it this way: somebody or something says to the Ledger, "Hey Ledger, calculate the final grades for all the Students you have listed in you" and the Ledger goes to each LedgerRow and says "Hey, LedgerRow, calculate the final grade for the Student whose scores and attendance you are tracking."
Something like that. If you don't get it, ask some other bartenders or sheriffs to help you out as I'm going to be on the trail for a few days to drive some cattle. If, after all my pleading for you to forget about patterns, you are still bent on finding one to solve your "problem", then I wish you luck in your quest. So long for now!
I hate it when an idea pops into my head just as I'm about to fall asleep...
If you really want a pattern name to attach to all this, how about I give you two: "Delegation" and "Loop Iteration" Not sure if those are actually patterns though. At the very least, they are idioms, I suppose. Anyway, that might scratch your particular itch. Hopefully.