I have requirement like this that Driver
and Mechanic
can Start,ApplyBrakes,ChangeGear and Stop the Car
.
I also have 2 additional functionalities that is Mechanic
is the only one who can ChangeOil
and AdjustBrakes
and Driver
should not be able to do this.
Based on this,this is what I have prepared :
interface IDrivable
{
void Start();
void ApplyBrakes();
void ChangeGear();
void Stop();
}
interface IFixable
{
void ChangeOil();
void AdjustBrakes();
}
public class Car : IDrivable, IFixable
{
public void AdjustBrakes()
{
throw new NotImplementedException();
}
// implement all the methods here
public void ApplyBrakes()
{
throw new NotImplementedException();
}
public void ChangeGear()
{
throw new NotImplementedException();
}
public void ChangeOil()
{
throw new NotImplementedException();
}
public void Start()
{
throw new NotImplementedException();
}
public void Stop()
{
throw new NotImplementedException();
}
}
class Driver
{
private IDrivable car;
public Driver(IDrivable car)
{
this.car = car;
}
public void driveCar()
{
this.car.Start();
this.car.ChangeGear();
this.car.ChangeGear();
}
}
class Mechanic
{
private IDrivable _drivableCar;
private IFixable _fixableCar;
public Mechanic(IDrivable drivableCar, IFixable fixableCar)
{
this._drivableCar = drivableCar;
this._fixableCar = fixableCar;
}
public void driveCar()
{
this._drivableCar.Start();
}
}
class Program
{
static void Main(string[] args)
{
var driver = new Driver(new Car());
driver.driveCar();
var mechanic = new Mechanic(new Car(), new Car());
}
}
I have a confusion here so as to whether I should add little abstraction
to represent Driver and Mechanic or not.Something like this :
abstract class User
{
public abstract void DriveCar();
}
class Mechanic : User { }
class Driver : User { }
If yes than what will be the benefit of having this abstraction
and How it will be helpful?
Another question is when does it make sense to have this sort of abstraction
?
I would also like to have feedback about this design if there is any .
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…