并行继承定义的具体例子
Concrete example for the definition of parallel inheritance
在阅读 Alexander Shvets 的Dive Into Design Patterns 时,我在“Favor Composition Over Inheritance”一节中偶然发现了以下陈述:
Trying to reuse code through inheritance can lead to creating parallel inheritance hierarchies
根据this site the definition of parallel inheritance is the situation in which subclassing a class requires creating yet another subclass elsewhere. I'm interested in knowing what would be this kind of scenario, where we'd have to subclass all over the place, and further more the why of it: why would we have to create the subclass elsewhere? Does the need arise from the context and the problem we are trying to solve, or is it induced by the structure of the (at least) two class hierarchies and composition between them? While here试图给出并行继承的数学定义,我不清楚其含义的必要性。
我是这么理解的。假设你有
public abstract class CarBase
{
// all cars run on liquid fuel, right? This is 1955
public decimal FuelVolume { get; set; }
}
然后你继承它并创建你的PUTruck
、SportsCar
、Sedan
等
突然之间,现在是 2022 年,你有了电动汽车。你可能会做
public abstract class ElectricCarBase : CarBase
{
public decimal ChargeVolume { get; set; }
}
^^ 这将伴随着未使用和不需要的属性的所有肮脏,一堆噪音,如防冻剂和燃油管路。你最终会并行继承。您将需要创建各种适配器来支持所有这些..
输入“组合继承”
public abstract class CarBase
{
public List<IFuelProvider> FuelSources { get; set; }
}
public interface IFuelProvider
{
public FuelType TypeOfFuel { get; set; }
public string MeasureUnit { get; set; }
public int FuelUnits { get; set; }
}
现在,您可以支持燃气、电动或混合动力
这是我的理解。欢迎不同意
说到继承,似乎总能拿动物王国来举例子。所以我们有一个 Animal
的 class 层次结构,就像这样。
interface Animal {
void eat(Food someFood);
}
但是每个 Animal
都有自己的特殊 Food
。所以当我们用Dog
替换classAnimal
时,我们需要用DogFood
替换classFood
,当我们用classAnimal
和 Cat
我们需要用 CatFood
代替 class Food
等等。
并行层次结构可以自然地出现在问题域中,在这种情况下,在代码中以相同的方式对它们进行建模可能是明智的。但是并行层次结构也可以人为地出现在解决方案域中,并且这种冗长可能是不可取的。
在 Whosebug 上,这种情况经常出现的句法问题是,如何确保我的动物没有吃错食物?
在阅读 Alexander Shvets 的Dive Into Design Patterns 时,我在“Favor Composition Over Inheritance”一节中偶然发现了以下陈述:
Trying to reuse code through inheritance can lead to creating parallel inheritance hierarchies
根据this site the definition of parallel inheritance is the situation in which subclassing a class requires creating yet another subclass elsewhere. I'm interested in knowing what would be this kind of scenario, where we'd have to subclass all over the place, and further more the why of it: why would we have to create the subclass elsewhere? Does the need arise from the context and the problem we are trying to solve, or is it induced by the structure of the (at least) two class hierarchies and composition between them? While here试图给出并行继承的数学定义,我不清楚其含义的必要性。
我是这么理解的。假设你有
public abstract class CarBase
{
// all cars run on liquid fuel, right? This is 1955
public decimal FuelVolume { get; set; }
}
然后你继承它并创建你的PUTruck
、SportsCar
、Sedan
等
突然之间,现在是 2022 年,你有了电动汽车。你可能会做
public abstract class ElectricCarBase : CarBase
{
public decimal ChargeVolume { get; set; }
}
^^ 这将伴随着未使用和不需要的属性的所有肮脏,一堆噪音,如防冻剂和燃油管路。你最终会并行继承。您将需要创建各种适配器来支持所有这些..
输入“组合继承”
public abstract class CarBase
{
public List<IFuelProvider> FuelSources { get; set; }
}
public interface IFuelProvider
{
public FuelType TypeOfFuel { get; set; }
public string MeasureUnit { get; set; }
public int FuelUnits { get; set; }
}
现在,您可以支持燃气、电动或混合动力
这是我的理解。欢迎不同意
说到继承,似乎总能拿动物王国来举例子。所以我们有一个 Animal
的 class 层次结构,就像这样。
interface Animal {
void eat(Food someFood);
}
但是每个 Animal
都有自己的特殊 Food
。所以当我们用Dog
替换classAnimal
时,我们需要用DogFood
替换classFood
,当我们用classAnimal
和 Cat
我们需要用 CatFood
代替 class Food
等等。
并行层次结构可以自然地出现在问题域中,在这种情况下,在代码中以相同的方式对它们进行建模可能是明智的。但是并行层次结构也可以人为地出现在解决方案域中,并且这种冗长可能是不可取的。
在 Whosebug 上,这种情况经常出现的句法问题是,如何确保我的动物没有吃错食物?