类型或抽象的实现......优点和缺点

时间:2022-09-25 16:09:49

I was talking with my fellow Java programmers, and came across a novice question, polemic and perhaps even silly, which unfortunately confuse me. In order for my question becomes understandable, I'll give a quick example here. Consider the following situation:

我正在与我的Java程序员交谈,并遇到了一个新手问题,辩论,甚至可能是愚蠢的,不幸的是让我感到困惑。为了让我的问题变得可以理解,我将在这里给出一个简单的例子。考虑以下情况:

I have a car engine. Every engine is designed to work with one type of fuel (named combustible).

我有一个汽车引擎。每个发动机都设计用于一种燃料(称为可燃物)。

In this type of problem, I created a "Vehicle" class, which uses a "Engine" as a class field member, because every engine has properties and behaviors. Let's suppose that the car fuel container is part of the car engine, therefore, an engine knows how to fuel itself, and knows how to store it. With these things in mind, I sought to develop an "Engine" class (as said before), which has such properties. The problem is basically this. I get to be in doubt when I think of the following:

在这类问题中,我创建了一个“Vehicle”类,它使用“Engine”作为类字段成员,因为每个引擎都有属性和行为。让我们假设汽车燃料容器是汽车发动机的一部分,因此,发动机知道如何给自己加油,并知道如何储存它。考虑到这些因素,我试图开发一个“引擎”类(如前所述),它具有这样的属性。问题基本上就是这个。当我想到以下内容时,我会产生疑问:

There are several types of car engines. Different engines have the same properties, but configured differently. Supposing that engine models do not suffer polymorphism, I got two different types of designs...

有几种类型的汽车发动机。不同的引擎具有相同的属性,但配置不同。假设引擎模型没有遭受多态性,我得到了两种不同类型的设计......

I will call this "model A":

我称之为“模特A”:

类型或抽象的实现......优点和缺点

And this, "model B":

而这,“模型B”:

类型或抽象的实现......优点和缺点

Note that model A shows a concrete class "Engine", while model B demonstrates an abstract class "Engine" with it's implementations. My question is what are the implications, advantages and disadvantages present among different types of models.

注意,模型A显示具体类“Engine”,而模型B显示带有它的实现的抽象类“Engine”。我的问题是不同类型的模型之间存在什么含义,优点和缺点。

I was always instructed by programmers, analysts, and database administrators to never tie data into programming code, and never limit a program to be recompiled (over and over), where only the recompilation offer results.

程序员,分析师和数据库管理员总是指示我永远不要将数据绑定到编程代码中,并且永远不要限制重新编译的程序(反复编译),只有重新编译提供结果。

According to model B, a change in "engine market" and companies will always reflect a change of code. As a consequence, this will lead to creation of a new type of engine, its extension of the "Engine" class, its implementation, and compilation.

根据模型B,“引擎市场”和公司的变化将始终反映代码的变化。因此,这将导致创建一种新类型的引擎,它的“引擎”类的扩展,它的实现和编译。

However, the model "A" shows that it is difficult to distribute the same engine for different vehicles, for example, with the same model. The creation of each new instance of an engine equal to another may require the same settings as necessary.

然而,模型“A”表示难以为不同的车辆分配相同的发动机,例如,使用相同的模型。每个新引擎实例的创建等于另一个可能需要相同的设置。

I see positives and negatives in both types of models, but I'm not sure of them. This situation proves to me that the addition of new types of things can perhaps lead to a bad road.

我看到两种模型都有正面和负面,但我不确定它们。这种情况向我证明,添加新类型的东西可能会导致糟糕的道路。

Although I want to have made ​​clear the example of engines and cars, I think this situation can be seen elsewhere. Thinking of another kind of example quickly ... if we have a "Fruit" class with subtypes "orange", "grape", "Etc." for each new fruit will we have to necessarily rewrite the program and recompile it? This does not tie up a program?

虽然我想明确发动机和汽车的例子,但我认为这种情况可以在其他地方看到。快速思考另一种例子...如果我们有一个“水果”类,其子类型为“橙色”,“葡萄”,“等等”。对于每个新的水果,我们必须重写程序并重新编译吗?这不会占用一个程序?

If I'm confusing something, please objectify it as answer.

如果我对某些事情感到困惑,请将其客观化为答案。

2 个解决方案

#1


0  

If different subtypes require different code to handle them, make them different subclasses. If different types differ only by the value of one or more fields, and you don't have to write any special code to handle any of those values differently, just make them all instances of a single concrete class.

如果不同的子类型需要不同的代码来处理它们,请将它们设为不同的子类。如果不同类型的区别仅在于一个或多个字段的值,并且您不必编写任何特殊代码来以不同方式处理任何这些值,只需将它们作为单个具体类的所有实例。

If you don't need subclasses now, but you're worried that you might in the future, don't worry. If your requirements change so that you need subclasses in the future, you'll undoubtedly have to change your code in other ways anyway, so you'll have to recompile anyway.

如果您现在不需要子类,但您担心将来可能会担心,请不要担心。如果您的需求发生变化,以便将来需要子类,那么无论如何您无需以其他方式更改代码,因此无论如何都必须重新编译。

#2


0  

Unless a model is fundamentally flawed, in general, there are no positives or negatives worth talking too unless a context is provided. By context, I mean a set of requirements. A set of requirements will make model A the ideal one, and another would invalidate it completely (same for B.)

除非模型存在根本缺陷,否则除非提供上下文,否则没有任何积极或消极的话值得讨论。根据上下文,我的意思是一组要求。一组要求将使模型A成为理想的一个,而另一个要求将完全无效(对于B而言)

In the most general terms, model A's supposed disadvantage is that it models a concrete set of requirements. Namely that there are no requirements to extend the design. There will always be an engine with set-in-stone characteristics and capabilities. The requirements must explicitly call for this.

在最一般的术语中,模型A的假设缺点是它模拟了一组具体的要求。即没有要求扩展设计。始终存在具有固定特性和功能的发动机。要求必须明确要求这一点。

In some real cases, this would be the ideal model.

在一些实际情况中,这将是理想的模型。

In another hypothetical, there could be a family of engines, some made out of aluminum, others with different allows. Others might have 'plug-in' ports to attach different electronics that are not required in the base model.

在另一个假设中,可能有一系列发动机,一些由铝制成,另一些则具有不同的允许。其他人可能有“插件”端口来连接基本型号不需要的不同电子设备。

In such a case, then model B is the correct way to go (and model A would be a bad choice.)

在这种情况下,模型B是正确的方法(模型A将是一个糟糕的选择。)

So, again, a non-trivial discussion is hard to have without having requirements. Requirements on the business domain are what drives the most important characteristics of the domain model, which then drive the software and architecture related models.

因此,如果没有要求,很难进行非平凡的讨论。对业务领域的要求是驱动域模型最重要特征的因素,然后驱动软件和体系结构相关的模型。

ps.

Your model is mixing volatile state (on and possibly name) with type/structural attributes (combustible,capacity`, etc.)

您的模型将易失性状态(开启和可能名称)与类型/结构属性(可燃物,容量等)混合在一起

Also, as you describe the problem, fuel container needs to be taken into account. But in reality, it is not part of engine. It is an aggregate (not a composite but an aggregate), one that is attached to the engine, but that does not depend on it. It can be replaced, swapped and/or destroyed independently of the engine (and vice versa).

此外,在描述问题时,需要考虑燃料容器。但实际上,它不是引擎的一部分。它是一个聚合(不是复合而是聚合),一个附加到引擎,但不依赖于它。它可以独立于发动机进行更换,交换和/或销毁(反之亦然)。

Meaning, both the engine and the fuel container have different life cycles, even if both are joined into a working system. This would also call for a 'model B' type of system, for you can have engine specializations that can only be adapted to specific models of fuel containers.

这意味着,即使两者都连接到工作系统中,发动机和燃料容器都具有不同的寿命周期。这也需要“B型”系统,因为您可以拥有只能适应特定型号燃料容器的发动机专业化。

As such, the fuel() method would be more appropriate for a fuel container entity, rather than for the engine entity.

因此,燃料()方法更适合于燃料容器实体,而不是发动机实体。

My $0.02

#1


0  

If different subtypes require different code to handle them, make them different subclasses. If different types differ only by the value of one or more fields, and you don't have to write any special code to handle any of those values differently, just make them all instances of a single concrete class.

如果不同的子类型需要不同的代码来处理它们,请将它们设为不同的子类。如果不同类型的区别仅在于一个或多个字段的值,并且您不必编写任何特殊代码来以不同方式处理任何这些值,只需将它们作为单个具体类的所有实例。

If you don't need subclasses now, but you're worried that you might in the future, don't worry. If your requirements change so that you need subclasses in the future, you'll undoubtedly have to change your code in other ways anyway, so you'll have to recompile anyway.

如果您现在不需要子类,但您担心将来可能会担心,请不要担心。如果您的需求发生变化,以便将来需要子类,那么无论如何您无需以其他方式更改代码,因此无论如何都必须重新编译。

#2


0  

Unless a model is fundamentally flawed, in general, there are no positives or negatives worth talking too unless a context is provided. By context, I mean a set of requirements. A set of requirements will make model A the ideal one, and another would invalidate it completely (same for B.)

除非模型存在根本缺陷,否则除非提供上下文,否则没有任何积极或消极的话值得讨论。根据上下文,我的意思是一组要求。一组要求将使模型A成为理想的一个,而另一个要求将完全无效(对于B而言)

In the most general terms, model A's supposed disadvantage is that it models a concrete set of requirements. Namely that there are no requirements to extend the design. There will always be an engine with set-in-stone characteristics and capabilities. The requirements must explicitly call for this.

在最一般的术语中,模型A的假设缺点是它模拟了一组具体的要求。即没有要求扩展设计。始终存在具有固定特性和功能的发动机。要求必须明确要求这一点。

In some real cases, this would be the ideal model.

在一些实际情况中,这将是理想的模型。

In another hypothetical, there could be a family of engines, some made out of aluminum, others with different allows. Others might have 'plug-in' ports to attach different electronics that are not required in the base model.

在另一个假设中,可能有一系列发动机,一些由铝制成,另一些则具有不同的允许。其他人可能有“插件”端口来连接基本型号不需要的不同电子设备。

In such a case, then model B is the correct way to go (and model A would be a bad choice.)

在这种情况下,模型B是正确的方法(模型A将是一个糟糕的选择。)

So, again, a non-trivial discussion is hard to have without having requirements. Requirements on the business domain are what drives the most important characteristics of the domain model, which then drive the software and architecture related models.

因此,如果没有要求,很难进行非平凡的讨论。对业务领域的要求是驱动域模型最重要特征的因素,然后驱动软件和体系结构相关的模型。

ps.

Your model is mixing volatile state (on and possibly name) with type/structural attributes (combustible,capacity`, etc.)

您的模型将易失性状态(开启和可能名称)与类型/结构属性(可燃物,容量等)混合在一起

Also, as you describe the problem, fuel container needs to be taken into account. But in reality, it is not part of engine. It is an aggregate (not a composite but an aggregate), one that is attached to the engine, but that does not depend on it. It can be replaced, swapped and/or destroyed independently of the engine (and vice versa).

此外,在描述问题时,需要考虑燃料容器。但实际上,它不是引擎的一部分。它是一个聚合(不是复合而是聚合),一个附加到引擎,但不依赖于它。它可以独立于发动机进行更换,交换和/或销毁(反之亦然)。

Meaning, both the engine and the fuel container have different life cycles, even if both are joined into a working system. This would also call for a 'model B' type of system, for you can have engine specializations that can only be adapted to specific models of fuel containers.

这意味着,即使两者都连接到工作系统中,发动机和燃料容器都具有不同的寿命周期。这也需要“B型”系统,因为您可以拥有只能适应特定型号燃料容器的发动机专业化。

As such, the fuel() method would be more appropriate for a fuel container entity, rather than for the engine entity.

因此,燃料()方法更适合于燃料容器实体,而不是发动机实体。

My $0.02