"RSH" <
[email protected]> a écrit dans le message de (e-mail address removed)...
|I am still trying to grasp the use of real world Objects and how to
| conceptualize them using a business scenerio.
|
| What I have below is an outline that I am wrestling with trying to figure
| out a class structure:\
|
| Top level Objects:
| Companies
| Employees
|
| Under Companies I have:
| CompanyDeductions
| CompanyAccruals
| CompanyTaxes
|
| Under Employees I have:
| EmployeeDeductions
| EmployeeAccruals
| EmployeeTaxes
|
| Now my question is how should I setup my classes based on that information
| above?
| I originally thought about making Company the base Class and Have the
| CompanyDeductions,Accruals and Taxes inherit the Company Class, but I'm
not
| sure thats the way to go. My other thought then became just having The
| Deductions Accruals and Tax information as optional properties under the
| company so that the Employees, which belong to companies could inherit the
| company class.
|
| How would you suggest I go about setting up my class structure?
My guess is that you are very new, as was I, to OO design and, like I did,
you see everything as being solved by inheritance
There are no obvious inheritance relationships in the classes which you
describe, however there are other relationships that need to be clarifying.
I would declare the following classes :
Company
Employee
Deduction
Accrual
Tax
Note that all the names are singular and that the names do not apparently
link to each other, as in EmployeeAccrual.
Can I suggest your classes might look something like this (sorry, but I
don't speak VB)
public class Deduction
{
private string id;
private string name;
public Deduction(string id, string name)
{
this.id = id;
this.name = name;
}
public string ID
{
get { return id; }
}
public string Name
{
get { return name; }
}
}
public class Company
{
private string id;
private string name;
private string fein;
private ArrayList deductions;
public Company(string id, string name, string fein)
{
this.id = id;
this.name = name;
this.fein = fein;
}
public string ID
{
get { return id; }
}
public string Name
{
get { return name; }
}
public string Fein
{
get { return fein; }
}
public void AddDeduction(Deduction deduction)
{
if (deductions == null)
deductions = new ArrayList();
deductions.Add(deduction);
}
public void PrintDeductions()
{
// not exactly the same code as yours for simplicity of example
if (deductions == null || deductions.Count == 0)
{
Console.WriteLine("No Deductions exist for this company")
return;
foreach (Deduction deduction in deductions)
Console.WriteLine(deduction.ID + deduction.Name);
}
}
}
Note that the properties are readonly; this is because you have designed the
constructor to initialise all the fields, therefore indicating that this is
the normal way to setup an instance and that the instance, once created
should be immutable.
The AddDeduction(...) method takes a Deduction instance rather than the
parameters to create one; passing parameters rather than an object indicates
that a Deduction may only be created by a Company and cannot exist outside
of the Company. Passing an object parameter indicates that the Deduction can
exist outside of the Company.
So now your test code should look something like this :
void Main()
{
Company company1 = new Company("00000001", "Rons Test Co.", "12-3456789");
Company company2 = new Company("00000002", "Bills House of Bugs",
"98-3456789");
Deduction deduction = new Deduction("401k", "Company 401k Program");
company1.AddDeduction(deduction);
deduction = new Deduction("Uniform", "Company Uniforms");
company1.AddDeduction(deduction);
company1.PrintDeductions();
company2.PrintDeductions();
Console.ReadLine();
}
As you can see, no inheritance is required.
Joanna