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

时间:2012-07-13 23:23:28

标签: java uml composition class-diagram

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

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()那样。我没有包含有关accele()方法的任何内容,它没有关系。

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

1 个答案:

答案 0 :(得分:3)

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

注意:我将Engine添加到图表中,作为“protected”和“Class”前缀,以避免混淆。

(1)构图>

“CarClass”的一个对象,由1个“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回答

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

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

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

3.1 Main Paralell Hierarchy Diagram

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

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

3.2附加的Paralell层次图

..............................................................................
..+---------------------------------------+........+-------------------------+..
..|             <<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

干杯。

P.S。我可以,你是危险的Fishburguer吗?