views:

18

answers:

1

Hi,

I’m currently in the process of developing a domain model for a new application and have reached the stage where I need to define relationships with classes of different types that may perform the same role, and am confused as to the best way to define the relationship.

For example:

public class Car
{
    public IDriver driver { get; set;}
    public IPassenger passenger { get; set; }
}

public class Person : IDriver, IPassenger
{
}

public class Pet : IPassenger
{
}

In this case I want to be able to define that a diver of a car can be any class that implements IDriver and any passenger must implement IPassenger. In the case of a passenger it could be with a person or a pet. The interfaces in essence are defining the roles of each class.

What I want to understand is if in peoples opinions this is a good approach, or if the same thing could be accomplished using a different mechanism.

A: 

Hard to say with the information posted in the question...

Start with behavior, an acceptance test or a scenario of use. (I don't want to make up a scenario from thin air.)

Roles are relationships between collaborating objects. e.g. Object B may be a dependency, listener or a policy/strategy of Object A.

So now you need to design from Object A's perspective i.e. Outside-in. e.g. What does Car expect from its driver ? Those behaviors would translate to members of the Driver Role.

Gishu