关于在我的设计中是否有继承的困惑 [英] Confusion regarding having inheritance or not in my design

查看:13
本文介绍了关于在我的设计中是否有继承的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的要求,DriverMechanic 可以Start,ApplyBrakes,ChangeGear and Stop the Car.

I have requirement like this that Driver and Mechanic can Start,ApplyBrakes,ChangeGear and Stop the Car.

我还有 2 个附加功能,即 Mechanic 是唯一可以 ChangeOilAdjustBrakesDriver 的功能> 应该无法做到这一点.

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());
        }
    }

我在这里有点困惑,不知道是否应该添加一点抽象来表示Driver和Mechanic.类似这样的:

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 { }

如果是,那么拥有这个 abstraction 有什么好处?它会有什么帮助?

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 .

谢谢

推荐答案

你没有抽象的设计是正确的,看起来不错.但是,如果您想添加抽象级别,我不会使用您提供的示例,因为正如您所怀疑的那样,它不会给整体设计带来任何好处.

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.

你可以做的是让 Mechanic 类从 Driver 类继承,因为我认为毕竟机械师必须是司机.这样做,如果 Driver 类必然会发生变化,您将简化未来的工作.

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.

这篇关于关于在我的设计中是否有继承的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆