Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

c# - Confusion regarding having inheritance or not in my design

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...