简体   繁体   English

Java中使用类图的聚合和组合实现

[英]Aggregation and Composition Implementation in java using Class Diagram

I am trying to understand the aggregation and Composition. 我试图了解聚合和组成。
Suppose I have something like below: 假设我有以下内容:
在此处输入图片说明

and I want to implement it using java, is the below implementation correct ? 并且我想使用java来实现,下面的实现正确吗?

public class ClassC { private String z; }  

public class ClassB { 
   private String y; 
   private ClassC classC;
   //-----setter and getter for classC
}   


public class ClassA {
   private String x;
   private List<ClassB> classBList;
   public ClassA(final List<ClassB> classBList) {
      this.classBList=classBList
   }
}  

Also, how to ensure that ClassB can have exactly 1 ClassC ? 另外,如何确保ClassB可以恰好具有1个ClassC?
and ClassA can have 1 or more ClassB ? 并且ClassA可以有1个或多个ClassB? as marked on the arrows(if I understand these notations correctly). 如箭头所示(如果我正确理解了这些符号)。

I think what might be confusing is the difference between composition and aggregation as both are a sample of "has a" relation. 我认为可能令人困惑的是组合和聚合之间的区别,因为两者都是“具有”关系的示例。 However, composition is stronger than aggregation, the containing object controls the entire lifecycle of the part object. 但是,组合比聚合更强大,包含对象控制着部分对象的整个生命周期。

You could write it with final as you did but it doesn't quite hit the mark: 您可以像做final那样用final编写它,但效果并不理想:

public class ClassA {
   private String x;
   private final List<ClassB> classBList;
   public ClassA(String x, List<ClassB> classBList) {
      this.classBList=classBList;
      this.x = x;
   }
}  

I think this would make for a clearer representation: 我认为这将使表述更加清晰:

public class ClassA{
    private String x;
    private final List<ClassB> classBList;
    public ClassA(String x){
       this.x = x;
       classBList = new ArrayList<ClassB>(2);
       classBList.add(new ClassB(..........));
       classBList.add(new ClassB(..........));
   }

}

You can model it this way, using your class diagram as an example. 您可以以类图为例,通过这种方式对其进行建模。 For ClassA, do as mihaisimi does: instantiate a list of ClassB in ClassA's constructor. 对于ClassA,请执行mihaisimi的操作:在ClassA的构造函数中实例化ClassB的列表。 If you want to determine exactly how many ClassB instances should be a part of a given ClassA instance, just add an integer parameter to the constructor and throw an exception if it's a 0. Then use the value passed as a loop counter for the Add method. 如果要确切确定给定ClassA实例应包含多少个ClassB实例,只需将一个整数参数添加到构造函数中,并在它为0时引发异常。然后将作为循环计数器传递的值用于Add方法。

For ClassB, add a parameter to its constructor of type ClassC. 对于ClassB,将参数添加到其ClassC类型的构造函数中。 So, something like this (java's a bit rusty, so feel free to correct my syntax as needed): 因此,如下所示(java有点生锈,因此可以根据需要随时更正我的语法):

public class ClassA
{
    public final List<ClassB> classBList;
    public ClassA(int y){
        this.myClassBList = x;
        classBList = new ArrayList<ClassB>(y);
        for(int i=0;i<y;i++)
        {
             classBList.add(new ClassB(new ClassC));
        }
}

public class ClassB
{
    public ClassC myClassCInstance; 
    public ClassB(ClassC myClassC)
    {
        myClassCInstance = myClassC;
    }
}

As you can see, this allows you to have any number of ClassB instances associated with a ClassA instance, and ties their lifetime to ClassA as composition requires. 如您所见,这使您可以将任意数量的ClassB实例与ClassA实例关联,并将它们的生存期与ClassA关联起来。 Also, as you can see, you can't add more than one ClassC instance to any instance of ClassB, and ClassB's lifetime isn't strictly coupled to ClassB's. 而且,如您所见,您不能向一个ClassB实例添加多个ClassC实例,并且ClassB的生存期并不严格地与ClassB关联。

So the basic difference in how to model composition and construction is this: for composition, instantiate the composed object in the composing object's (the one with the diamond) constructor, and for aggregation, pass the aggregated object into the aggregating object's constructor as a parameter. 因此,如何对组成和构造进行建模的基本区别在于:对于组成,在组成对象(带有菱形的对象)构造函数中实例化组成对象,对于聚合,将聚集对象作为参数传递给聚集对象的构造函数。

There are many correct ways to implement such a class diagram. 有许多实现这种类图的正确方法。

But in order to choose the correct implementation you should first make sure you understand the concepts of Association, Aggregation and Composition as they are defined in UML. 但是,为了选择正确的实现,您首先应该确保您了解UML中定义的关联,聚合和组合的概念。

I've written an article about that on my website: UML Composition vs Aggregation vs Association 我已经在我的网站上写了一篇关于该主题的文章: UML构成,聚合与关联

In short, the Composition is a type of Association with real constraints and impact on development, whereas the Aggregation is purely a functional indication of the nature of the Association with no technical impact. 简而言之,组合是一种具有实际约束并影响发展的协会类型,而聚合纯粹是该协会性质的功能性指示,而没有技术影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM