gpt4 book ai didi

java - 具有类层次结构的 UML 类图组合

转载 作者:搜寻专家 更新时间:2023-10-31 20:22:12 24 4
gpt4 key购买 nike

我想知道我是否可以按照我所做的方式对下面两个类层次结构之间的关系进行建模:

car-engine composition relationship

表示此含义的代码如下:

public abstract class Car
{
protected Engine engine;
}
public class StreetCar extends Car
{
public StreetCar()
{
engine = new StreetEngine();
}
}

...与 OffroadCar 类类似,它会执行类似 engine = new OffroadEngine() 的操作。我没有包含任何关于 accelerate() 方法的内容,它不相关。

我只想知道构图是否正确建模,或者添加那么多构图箭头是否多余甚至是错误的。

最佳答案

在回答问题之前,这是其中一种情况,将层次关系与组合关系分开是一个好主意,就好像它们在不同类型的图表中一样。

注意:我在图中添加了 Engine 作为“ protected ”和“类”前缀,以避免混淆。

(一)组成图

“CarClass”的对象由“EngineClass”的 1 个对象组成。

..............................................................................
..+-------------------------------------+........+-------------------------+..
..| <<abstract>> |........| <<abstract>> |..
..| CarClass |........| EngineClass |..
..+-------------------------------------+........+-------------------------+..
..| [#] Engine: EngineClass |<*>-----+ [+] startEngine(): void |..
..+-------------------------------------+........+-------------------------+..
..| [+] acelerate(): void <<virtual>> |.....................................
..+-------------------------------------+.....................................
..............................................................................

(2.1)继承关系图

在某些情况下,“CarClass”的对象可能有子类。

...........................................
..+-------------------------------------+..
..| <<abstract>> |..
..| CarClass |..
..+-------------------------------------+..
..| [#] Engine: EngineClass |..
..+-------------------------------------+..
..| [+] acelerate(): void <<override>> |..
..+-----------------+-------------------+..
.................../.\.....................
................../...\....................
.................+--+--+..............................................................
....................|.................................................................
....................+------------------------------------------+......................
....................|..........................................|......................
..+-----------------+-------------------+....+-----------------+-------------------+..
..| <<concrete>> |....| <<concrete>> |..
..| StreetCarClass |....| OffRoadCarClass |..
..+-------------------------------------+....+-------------------------------------+..
..| [+] acelerate(): void <<override>> |....| [+] acelerate(): void <<override>> |..
..+-------------------------------------+....+-------------------------------------+..
......................................................................................

(2.2)继承关系图

在某些情况下,“EngineClass”的对象可能有子类。

...........................................
..+-------------------------------------+..
..| <<abstract>> |..
..| EngineClass |..
..+-------------------------------------+..
..| [+] acelerate(): void <<override>> |..
..+-----------------+-------------------+..
.................../.\.....................
................../...\....................
.................+--+--+..............................................................
....................+------------------------------------------+......................
....................|..........................................|......................
....................|..........................................|......................
..+-----------------+-------------------+....+-----------------+-------------------+..
..| <<concrete>> |....| <<concrete>> |..
..| StreetEngineClass |....| OffRoadEngineClass |..
..+-------------------------------------+....+-------------------------------------+..
..| [+] startEngine(): void <<override>>|....| [+] startEngine(): void<<override>> |..
..+-------------------------------------+....+-------------------------------------+..
......................................................................................

3 个回答

现在,这是一种情况,其中一个类有一个至少一个组成成员,并且当主类被重写时,它的类型可能被重写。这有时称为“Paralell Hierarchy Software Pattern”或“Dual Hierarchy Software Pattern”。

您只提到了每个主类的 2 个子类,但实际上可能有更多。

通常我会用这两种方式制作图表。第一,我制作了第一个图表,添加了一条注释,表明是这种情况。

3.1 主要并行层次结构图

..............................................................................
..+-------------------------------------+........+-------------------------+..
..| <<abstract>> |........| <<abstract>> |..
..| CarClass |........| EngineClass |..
..+-------------------------------------+........+-------------------------+..
..| [#] Engine: EngineClass |<*>-----+ [+] startEngine(): void |..
..+-------------------------------------+........+------------+------------+..
..| [+] acelerate(): void <<virtual>> |.....................|...............
..+--------------+----------------------+.....................|...............
.................|............................................|...............
.................+--------------------------------------------+...............
.................|............................................................
........+--------+-------+....................................................
........| Note: |....................................................
........| Paralell /....................................................
........| Hierarchy /| ...................................................
........| / |....................................................
........+-------------/--+....................................................
..............................................................................

第二种情况,当两个类都有都添加成员的子类时。

3.2 额外的并行层次结构图

..............................................................................
..+---------------------------------------+........+-------------------------+..
..| <<concrete>> |........| <<concrete>> |..
..| OffRoadCarClass |........| OffRoadEngineClass |..
..+---------------------------------------+........+-------------------------+..
..| [+] createEngine(): void <<override>> |<*>-----+ [+] startEngine(): void |..
..+---------------------------------------+........| [+] nitroLevel(): void |..
..| [+] useNitro(): void |........+------------+------------+..
..| [+] acelerate(): void <<override>> |.....................|...............
..+--------------+------------------------+.....................|...............
.................|..............................................|...............
.................+----------------------------------------------+...............
.................|............................................................
........+--------+-------+....................................................
........| Note: |....................................................
........| Paralell /....................................................
........| Hierarchy /| ...................................................
........| / |....................................................
........+-------------/--+....................................................
..............................................................................

如果有必要,我可能会添加额外的图表。

4 显示代码

为了管理这种“平行”层次结构,通常会通过重写方法管理复合成员的创建。

public abstract class EngineClass
{
public void startEngine() { ... }
} // EngineClass

public abstract class CarClass
{
protected EngineClass engine;

public CarClass()
{
// ...
}

public EngineClass createEngine()
{
EngineClass Result = new EngineClass();
return Result;
}

public void start()
{
this.Engine = createEngine();
}
} // CarClass

public class StreetCarClass extends CarClass
{
public StreetCarClass()
{
// ...
}

@override
public EngineClass createEngine()
{
EngineClass Result = new StreetCarEngineClass();
return Result;
}
} // StreetCarClass

public class OffRoadCarClass extends CarClass
{
public OffRoadCarClass()
{
// ...
}

@override
public EngineClass createEngine()
{
EngineClass Result = new OffRoadCarEngineClass();
return Result;
}
} // OffRoadCarClass

public class ExampleClass
{
public static main()
{
EngineClass OffRoadCar = new OffRoadCarClass();
OffRoadCar.start();
}
} // OffRoadCarClass

干杯。

附言我可以,你,有一个 Fishburguer 吗?

关于java - 具有类层次结构的 UML 类图组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11479574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com