Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inheritance question 1

Status
Not open for further replies.

judithbarer

Programmer
Feb 9, 2007
2
US
I am designing a system trying to use object oriented design. I have a question about inheritance. I work for a kosher certifying agency. The agency has employees that work in the office or inspect factories. The same employee can do both. There are also employees who are called administrators who can also be inspectors. I thought I would a class called employee and then two classes that would inherit from employee called inspector and administrator.Is there any problem from a OO design standpoint if the same employee can be both and also be an employee at the same time?

Thanks
Judy
 
this scenario is similar to user/roles; where a user can have many roles and roles can have many users.
Code:
interface User
{
  void RemoveRole(Role role);
  void AddRole(Role role);
  bool IsInRole(Role role);
}
it's usually good practice to favor composition over inheritance.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
If it was me designing the system, I would draw a data model out also to show how the employees and the different sub-types all relate together; this may help you to identify what is common, what is inherited, and (possibly) additional class definitions required.

Greg
 
I think Jason is right, this model is widely used for managing permissions. In the future if you need to change or add some new types of permissions all you have to do is to add a Role. In most cases you don't even need to add a new class.

From the data model perspective the role represent just an attribute of the user.

-------------------------------------------

Design Patterns and Principles
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top