Confusion regarding having inheritance or not in my design

故事扮演 提交于 2019-12-13 09:20:00

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/56596666/confusion-regarding-having-inheritance-or-not-in-my-design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!