OOP object collections



Hi,

I have a basic question around how to handle collections of objects when
using object composition.

In my sample below I have a typical arrangement, I have a Company class and
a Employee class. My question is whether I should create a collection IN the
company class to store the employees, or as in my sample below, create a
EmployeeCollection class which is responsible for managing the collection of
employees?

It seems like it is a better construct to have a seperate class to manage
the collection...am I wrong there?

Thanks,
Ron

public class Company
{
private int m_Id;
private string m_Name;
private EmployeeCollection m_EmployeeCollection = null;

public Company(int Id, string Name)
{
m_Id = Id;
m_Name = Name;
LoadEmployees();
}

public int Id { get { return m_Id;}}
public string Name { get { return m_Name; } set { m_Name = value; } }

protected void LoadEmployees()
{
Employee employee = null;
m_EmployeeCollection = new EmployeeCollection(this);
// Load employees from datasource into EmployeeCollection object
employee = new Employee(this, 0, "Smith", "Doug");
m_EmployeeCollection.Add(employee);
}
}

public class EmployeeCollection
{
private List<Employee> m_EmployeeList = new List<Employee>();
private Company m_Parent = null;

public EmployeeCollection(Company Parent)
{
m_Parent = Parent;
}

public IEnumerable<Employee> GetEmployees()
{
foreach (Employee employee in m_EmployeeList)
{
yield return employee;
}
}

public void Add(Employee employee)
{
m_EmployeeList.Add(employee);
}

public void Remove(int Id)
{
foreach (Employee employee in m_EmployeeList)
{
if (employee.Id == Id)
{
m_EmployeeList.Remove(employee);
}
}
}
}

public class Employee
{
private int m_Id;
private string m_LastName;
private string m_FirstName;
private Company m_Parent = null;

public Employee(Company Parent,int Id, string LastName, string FirstName)
{
m_Id = Id;
m_LastName = LastName;
m_FirstName = FirstName;
m_Parent = Parent;
}

public int Id { get { return m_Id;}}
public string LastName { get { return m_LastName; } set { m_LastName =
value; } }
public string FirstName { get { return m_FirstName; } set { m_FirstName =
value; } }
}


.



Relevant Pages

  • Re: data transporting questions
    ... > public class Location implements java.io.Serializable { ... > private int locationId; ... > private String locationName; ... > public class Employee implements java.io.Serializable { ...
    (comp.lang.java.programmer)
  • data transporting questions
    ... private int locationId; ... private String locationName; ... public class Employee implements java.io.Serializable { ... The reason for creating these classes is that for each data request, ...
    (comp.lang.java.programmer)
  • Re: data transporting questions
    ... private int locationId; ... private String locationName; ... The reason for creating these classes is that for each data request, ... Taking Employee example sometimes the data request is only concerned ...
    (comp.lang.java.programmer)
  • Re: IT ethics in the workplace
    ... Is personal email on company computers private or not? ... } demolish its own buildings but that does not give every employee the right ... }>places your wife could've gotten fired for misuse of company equipment. ...
    (comp.security.misc)
  • Re: A simple employee class : OOP Study
    ... > | Basically I am envisioning a Class called Employee: ... > private string lastName; ... > public string LastName ... > private int quantity; ...
    (microsoft.public.dotnet.languages.csharp)

Loading