Search code examples
c#oopcoupling

Should an object searcher method be in the a parent object, or the same as the object beign searched?


Which constitutes better object oriented design?

Class User { 
   id {get;set} 
}
Class Office { 
   id {get;set}  
   List<User> Managers(){  }//search for users, return list of them
}

or this one

Class User { 
   id {get;set} 
   List<User> Managers(){ }//search for users, return list of them 
}
Class Office { 
   id {get;set}  
}

Solution

  • User john;
    List<User> managers = fred.Managers(); //get managers of this user
    
    Office london;
    List<User> managers = london.Managers(); //get managers of this office
    

    Unless it's a static method, make it a method of a class of which you have an instance: no point in making getUsers a non-static method of the User class, because then you'd need a user instance in order to invoke the getUsers method.