在回答问题之前,这是其中一种情况,将层次关系与组合关系分开是一个好主意,就好像它们在不同类型的图表中一样。
注意:我在图中添加了引擎,作为“受保护”和“类”前缀,以避免混淆。
(1) 组成图
“CarClass”的一个对象,它由一个“EngineClass”的单个对象组成。
..............................................................................
..+-------------------------------------+........+-------------------------+..
..| <<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 个回答
现在,这是一种情况,其中一个类有一个至少一个组成成员,并且当主类被覆盖时,它的类型可能被覆盖。这有时被称为“并行层次软件模式”或“双层次软件模式”。
您只提到了每个主类的 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
干杯。
PS我可以,你,haz一个Fishburguer吗?