是否使用has-a(composition)或is-a(继承)来建模汽车对象(及其部件如引擎)? [英] Whether to model a car object (and its parts such as engine) with has-a (composition) or is-a (inheritance)?

查看:285
本文介绍了是否使用has-a(composition)或is-a(继承)来建模汽车对象(及其部件如引擎)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含对象Car的类库。

I am developing a class library which will include the object Car.

这个困境是,Car本身将是一个类,包括注册号等字段汽车的一般信息。

The dilemma is, Car itself will be a class with fields such as Registration Number, and other general information on the car.

但一辆汽车有发动机,底盘等。这些物体也需要建模。他们应该是嵌在车内的课吗?如果没有,嵌入式类的使用场景是什么?

But a car has an engine, chassis, etc. These objects need to be modelled too. Should they be classes embedded within Car? If not, what is the usage scenario of an embedded class?

我了解到组合是一部分,因此您可以对单独的类进行建模并使用引擎类型,例如,在汽车的现场级别实现这一点。然而,聚合与ctor中传递的类型具有关系也适用(汽车具有引擎)。

I've learnt that composition is "part of", so you can model seperate classes and use the engine type, for example, at the field level of the car to achieve this. However, "aggregation", which is a "has a" relationship with the type being passed in the ctor, also applies (a car "has an" engine).

我去哪一个?

编辑:我目前正在做作业,因此我没有回复。课堂图书馆是一个基于汽车的网络应用程序。我是一个专业的开发人员(我在.NET中开发一个生活,但作为一个初级),所以这不是一个功课问题。

I am currently on homework hence the lack of a reply from me. The class library is for a web app based around cars. I am a professional developer (I develop in .NET for a living but as a junior) so this is not a homework question.

谢谢

推荐答案

类应该尽可能少的责任,并将其他功能封装并委派给其他类。许多小而简单的课程就是一个可读的,稳定的代码库的标志。

A class should have as few responsibilities as possible and encapsulate and delegate other functionality to other classes. Lots of a small, simple classes that do one thing is a sign of a readable, stable codebase.

是的,一辆车会有引擎, d建议使用这个接口和类似的有一个关系。再次,根据教授,你可能得到一个工厂创造不同的汽车的奖励积分(适当的,不?):

Yes, a car will "have" an engine, but I'd suggest using an interface for this and similar "has a" relationships. Again, depending on the professor, you might get bonus points for having a factory create different cars (appropriate, no?):

public class Car
{
    private Engine engine;
    public Car(Engine engine)
    {
        this.engine = engine;
    }

    public void accelerate()
    {
        this.engine.goFaster();
    }

    public void decelerate()
    {
        this.engine.goSlower();
    }

}

public interface Engine
{
    public void goFaster();
    public void goSlower();
}


public class ReallyFastEngine implements Engine
{
    public void goFaster()
    {
    // some code that goes really fast
    }
    public void goSlower()
    {
    // some code that goes slower
    }    
}

public class NotAsFastEngine implements Engine
{
    public void goFaster()
    {
    // some code that goes not as fast
    }
    public void goSlower()
    {
    // some code that goes slower
    }    
}

public class CarFactory()
{
    public static Car createFastCar()
    {
         return new Car(new ReallyFastEngine());
    }

    public static Car createNotAsFastCar()
    {
         return new Car(new NotAsFastEngine());
    }
}

这篇关于是否使用has-a(composition)或is-a(继承)来建模汽车对象(及其部件如引擎)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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