-3

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

I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • 1
    Eric Lippert wrote a lengthy multi-part blog post about something similar that might be of interest to you: [Wizards and warriors](https://ericlippert.com/2015/04/27/wizards-and-warriors-part-one/) –  Jun 14 '19 at 10:52
  • [tag:java] tag removed. Please avoid spamming programming language tags. The most relevant programming tag should be adequate -- or you could use language-agnostic tag if language really doesn't matter. – Hovercraft Full Of Eels Jun 14 '19 at 10:54
  • @elgonzo I have already read that but I am trying to understand this in context of my design.Also why close and downvote?What i am missing in the question? – I Love Stackoverflow Jun 14 '19 at 10:56
  • @HovercraftFullOfEels Sorry I wasnt aware of Language-agnostic tag and language doesnt matter that is why I have tagged both java and c# – I Love Stackoverflow Jun 14 '19 at 10:57
  • I didn't vote to close nor downvote, so i can't respond to that part of your question. With regard to the blog post i linked, note how it comes to the conclusion that a beneficial approach is to have the system and data structures (classes) model rules. Like you having rules here: "_A mechanic can adjust brakes_", "_A user can start engine, apply brakes, etc..._" –  Jun 14 '19 at 10:58
  • @elgonzo But in that blog where is this modelling rules specified?Could you please point out to that? – I Love Stackoverflow Jun 14 '19 at 11:00
  • It seems you missed to read the 5th and last part of this blog series... –  Jun 14 '19 at 11:02
  • Why is your mechanic driving one car and repairing another car? – Patrick Artner Jun 14 '19 at 11:29
  • @PatrickArtner Mechanic has the ability to drive the car as well as repair the car.Is there any design problem with respect to what you said? – I Love Stackoverflow Jun 14 '19 at 12:25
  • 1
    Your mechanic is testdriving one car instance and repairing another car instance. Why give him 2 different car instances? You can give him one car and he can see if / which interfaces the car fullfills – Patrick Artner Jun 14 '19 at 15:04
  • @PatrickArtner Oh yes sorry thats a good point.I missed it.Can you tell me something about abstraction that I have confusion with please ? – I Love Stackoverflow Jun 15 '19 at 05:46

1 Answers1

0

Your design without the abstraction is correct, it looks fine. However if you want to add an abstraction level, I would not use the example you provided because as you suspected, it does not bring anything to the overall design.

What you could do is make the Mechanic class inherit from the Driver class, because after all a mechanic has to be a driver, I think. In doing so, you would simplify future work, if the Driver class is bound to change.