2

I have a question about modeling associations between classes and interfaces. As far as I know, an interface specifies what an object can do; without providing the state or functionality (When to use an interface instead of an abstract class and vice versa?). Also, my book on OOAD (Object Oriented Modeling and Design by James Rubaugh)states that an association describes a group of links with common structure and common semantics, between object instances.

Now, suppose I have the following entities:

1) ICar Interface: Defines the operations a car can do

2) BMW : A class that realizes the ICar interface

3)IWheel : An interface defining the wheel capabilities

4) LuxuryWheel : A class that realizes the IWheel interface

Now, to model the relationship between BMW and LuuryWheel, which of the following do you think is correct, in a design perspective? I have shared my thoughts on each one

A) Create an association between ICar and Iwheel. BMW class can create concrete instances of LuxuryWheel class. This is highly flexible but couples car's capabilities with Wheel' s capabilities. Also, the definition of association says the relation is between instances.

B) Create an association between the BMW class and LuxuryWheel class. Solves the particular problem; but tightly couples BMW to Luxury wheels

C) Create an association between BMW class and Iwheel interface. This way BMW can use any type that realizes the IWheel interface.

Option C) looks better to me. Please share your thoughts.

Community
  • 1
  • 1
Pradeep
  • 299
  • 2
  • 8
  • 15

3 Answers3

6

I agree with Vladimir that, since you want to model cars and wheels with the help of interfaces, the association between them (which is actually a composition) should be modeled between the interfaces ICar and IWheel, as in the following diagram:

enter image description here

Since the classes BMW and LuxuryWheel realize the interfaces ICar and IWheel, they also need to realize/implement this association/composition, e.g. with the help of a 4-valued reference property BMW::wheels, or with the help of 4 different reference properties in BMW: leftRearWheel, rightRearWheel, leftFrontWheel, rightFrontWheel.

Gerd Wagner
  • 4,690
  • 1
  • 16
  • 39
  • 1
    +1 You can add a specified association between BMW and LuxuryWheel and add a <> relation between the abstract association and your association. That show that your relation is in fact the same relation as specified between the interfaces, but it has been refined to specific the implementations BMW and LuxuryWheel – Geert Bellekens Jan 19 '15 at 07:59
2

In order to get robust solution, create association between ICar and IWheel interfaces. It is possible , because interfaces are types. Connecting interfaces using association means, that any instance of classifier which realizes ICar interface must be associated to instance of classifier which realizes IWheel. You also define abstract classes for car and wheel and make association between them. The result will be similar.

Vladimir
  • 1,806
  • 11
  • 13
0

Simply speaking a car can support different type of motors. So you must think to an additional class that permit to add different type of motors. In this case relation between interfaces or classes must be do it with some additional interface.

Mihai8
  • 2,871
  • 1
  • 18
  • 27