Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
221 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...