Title is an abstraction of what I am actually doing, but in essence the same.
The main entity I will be dealing with are the Employee
s themselves; "Send package to employee John Doe". In order to work with a given Employee
, I need to use the IOffice
they belong to, and each office has a different way of delivering that package to that Employee
. I am not really interested in how the package is delivered, just that it gets there.
In the first way mentioned in the title, it would look something like this:
public class Employee
{
private IOffice _office;
public Name { get; set; }
public void Send(Package package)
{
_office.Send(package, this)
}
}
public class SomeOffice : IOffice
{
public void Send(Package package, Employee employee)
{
// Implementation of how this office gets the package to employee
}
}
Doing it this way, I can simply have a list of all employees, and employees in the same office can share the same IOffice
object.
The second approach like so:
public class Employee
{
public Name { get; set; }
}
public class SomeOffice : IOffice
{
public List<Employee> Employees { get; set; }
public void Send(Package package, Employee employee)
{
// Implementation of how this office gets the package to employee
}
}
This makes it harder to have instance of Employee
and send a package to it. You must get the instance you want from the Employees
-collection, and pass it into the office's Send-method. However, it keeps the Employee-class simpler, and makes the IOffice take care of everything.
There are probably other ways to go about this also. How would you do it?
Edit:
For concrete example, replace IOffice
with a CommunicationInterface
, such as SerialPort
, UdpClient
, TcpClient
etc.
At the end of each endpoint there are different Devices
(Employees
). These devices all behave similarly, but how to send them a request (Package
) and get a response back differs.
From my program, I want to have a list of all available devices, across all CommunicationInterfaces, and send requests to them, and get responses back.
Package
. After all, you send thePackage
, you're not "sending" anOffice
orEmployee
. Just my $0.02.Send
orReceive