Is there a good rule of thumb or test I can perform to determine whether a method or field belongs in a class? How can identify when a member does not belong?
I find that my single biggest stumbling block in object-oriented design is trying to figure out what goes where. It seems like there are far too many cases where the answer is: "it could go here or there."
Here's a quick example of the type of thing I'm struggling with:
Public Class ITDepartment
Private _sysadmins As List(Of Employee)
Private _developers As List(Of Employee)
// properties, public stuff...
Private Sub AddSkillToGroup(ByVal emps As List(Of Employee), ByVal skill As Skill)
For Each e As Employee In emps
e.AddSkill(skill)
Next
End Sub
End Class
The ITDepartment
object manages the 2 groups of Employees
...but should it know that Employees
have skills? Should a method like AddSkillToGroup
be relocated?
EDIT:
It seems that the consensus thus far is that the ITDepartment should not know about the employees' skills. I'll play Devil's advocate a bit to illustrate where my confusion comes into play.
The ITDepartment is composed of two Employee collections. Shouldn't it be able to delegate to those collection items? The AddSkill method still belongs to the Employee class. The ITDepartment is simply instructing its group of employees to add a skill to each of its members.