2

Say I have two classes

class Driver{
    //attributes of driver ,ex: driving licence number
    // methods related to driving ,ex: drive(Car) , stop(Car)
    changeTyre(Car,Tyre);   // sometimes the driver can change the tyres right?
}
class Mechanic{
    // Hard mechanical stuff , ex: repairEngine(Car)
    changeTyre(Car,Tyre);  // Simple.hence sometimes the driver also does
}

Now the implementations of the two changeTyre() methods will be the same.

Now I have two issues,

  1. There is a repetition (duplication) of code
  2. It doesn't seem meaningful to have a Super class containing the changeTyre(Car,Tyre) method

How these kind of situations handled?

Bart
  • 19,692
  • 7
  • 68
  • 77
Dinushan
  • 2,067
  • 6
  • 30
  • 47
  • 1
    What does `changeTyre(Car,Tyre)` do to be a class method? If the implementations are the same, I would expect that either it doesn't have to be a class method (no class attributes/methods are used), or both classes have more overlap than you seem to suggest (e.g. the person's name may be logged for changing the tyre: both classes have a name attribute) in which case there would be a meaningful base class (e.g. Person, or CarLover or something). – catchmeifyoutry Jul 24 '12 at 12:27
  • changeTyre(Car,Tyre) is a class method because it is a behavior of both the Driver and Mechanic(I guess I'm correct).I still not "convinced" to have a base class which contains the changeTyre(Car,Tyre) method:( – Dinushan Jul 24 '12 at 12:31
  • @D-Shan: It's also a behaviour of the car and tyre :) – user396672 Jul 24 '12 at 12:50
  • @user396672 :| but then how does it happen? I mean when that behavior in the Car will happen. ex: where a method invoke like `car.ChangeTyre(Tyre)` occurs :( . it will be there in both the Driver and Mechanic classes right? – Dinushan Jul 24 '12 at 12:57

3 Answers3

3

To expand on using composition over inheritance (Willie's answer), I think you were on the right track in your comment about using ChangeTyre like car.ChangeTyre(Tyre).

Each mechanic or driver will be associated to a car, so they can have a Car property -

class Driver{

    Car driverCar;

    //constructor
    Driver(Car car)
    {
        driverCar = car;
    }

    //attributes of driver ,ex: driving licence number
    // methods related to driving ,ex: drive(Car) , stop(Car)
    changeTyre(Tyre) 
    {
        driverCar.changeTyre(Tyre);
    }
}

They changeTyre method of Driver and Mechanic might be the same, but the actual logic to change the tyre will live in one place. I don't think inheritance works because a mechanic is not a driver, and inheritance supports an "is-a" relationship. It may not make sense to force changeTyre to a superclass (say Person), because not every class that inherits from Person would need to have a changeTyre method. See this question for more information about composition over inheritance.

Community
  • 1
  • 1
rosscj2533
  • 9,195
  • 7
  • 39
  • 56
  • ok.anyhow in compositions we can't help it having a little bit of repitition right? – Dinushan Jul 25 '12 at 05:20
  • @D-Shan - that's right. You can say that the Driver "delegates" the work of changing the tyre to the Car. The car is what really knows how to change a tyre. Maybe a AirportMechanic has an Airplane property, and his `changeTyre` delegates the work to the Airplane - `airplane.changeTypre(Tyre)` – rosscj2533 Jul 25 '12 at 15:15
1
class Driver
{
    //attributes of driver ,ex: driving licence number
    // methods related to driving ,ex: drive(Car) , stop(Car)
    public TyreFunction TyreFunctions { get; set; }
}

class Mechanic {
    // Hard mechanical stuff , ex: repairEngine(Car)
    // Simple.hence sometimes the driver also does
    public TyreFunction TyreFunction { get; set; }
}

class TyreFunction
{
    change(Car,Tyre)
} 

Do This !

Then you have no duplicated code and control in one class. Also you can create al list of functions to add specific functions to the classes.

Rob
  • 4,927
  • 12
  • 49
  • 54
0

I would suggest that Mechanic should actually inherit from Driver, as I am presuming that in your context anything a driver can do a mechanic will be able to do (i.e drive the car for testing).

The changeTyre method should be virtual so that it can be overriden by Mechanic as the implementation may well be different (different tools etc).

I would also suggest that some of the elements should be seperated into interfaces (methods for driving, car maintanence methods etc).

Alan Bradbury
  • 128
  • 2
  • 8
  • Well this seems to work.I hadn't figured out that the Mechanic is a Driver – Dinushan Jul 24 '12 at 13:08
  • 1
    @D-Shan it is possible that if you break Driver up into interfaces you'll find Driver is constructed of several elements. Can every driver really change a tyer etc? But what is the case is that a mechanic is a driver. – Alan Bradbury Jul 24 '12 at 13:16