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

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

问题描述

我这样要求DriverMechanic可以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());
        }
    }

我在这里是否应该添加一点abstraction来代表驾驶员和机械师感到困惑.

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?

另一个问题是,什么时候使用这种abstraction是有意义的?

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天全站免登陆