简体   繁体   English

类图中的构图关系可以有周期吗?

[英]composition relationship in class diagram can have cycles?

I want to implement a model checker for Java class diagrams. 我想为Java类图实现模型检查器。

There are many constraints that I've been considered for a while. 我已经考虑了很多限制了一段时间。

Hope anyone can help me figure it out. 希望任何人都能帮助我解决。

All the cycles here I mean are pure cycles with only one type of relationship. 我这里所说的所有循环都是纯循环,只有一种类型的关系。

Q1: Suppose class A is composed of class B, is it possible that class B is also composed of class A, given that class A and class B are different classes? 问题1:假设A类由B类组成,并且A类和B类是不同的类,那么B类是否也可能由A类组成吗? Moreover, is it possible for composition relationship to have cycles? 此外,构图关系是否可能具有周期?

Q2: What about other relationship in the class diagram, like aggregation, dependence and association? 问题2:关于类图中的其他关系,例如聚合,依赖关系和关联,该怎么办? What's the meaning of cycles in these relationships? 这些关系中的循环是什么意思? Can anyone give some examples? 谁能举一些例子?

Thanks for reading my question and hope someone can help me out. 感谢您阅读我的问题,希望有人能帮助我。

Q1: Suppose class A is composed of class B, is it possible that class B is also composed of class A, given that class A and class B are different classes? 问题1:假设A类由B类组成,并且A类和B类是不同的类,那么B类是否也可能由A类组成吗? Moreover, is it possible for composition relationship to have cycles? 此外,构图关系是否可能具有周期?

Strictly speaking in UML terms... yes, but you'd be hard pressed to actually implement this in code. 严格说来是UML术语...是的,但是很难在代码中实际实现。 If you ask yourself, "can B stand alone without A?" 如果你问自己:“如果没有A,B可以独自站立吗?” and "can A stand alone without B?" 和“ A可以没有B独立吗?” If you can answer no to both of those at the same time, then you can have two classes composed of each other. 如果您不能同时对这两个答案都回答“否”,那么您可以使两个类相互组成。 Since one would have to be able to stand on its own for the other to be composed of it, you can't have both. 因为一个人必须能够独立站立,而另一个人才能由它组成,所以您不能同时拥有两者。 However, since composition vs aggregation is largely based on design and context, it's not completely impossible. 但是,由于组合与聚合主要基于设计和上下文,因此并非完全不可能。 You can for instance, have something like this: 例如,您可以具有以下内容:

Class B contains a reference to A , and A contains a reference to B B包含对A的引用,而A包含对B的引用

public class A {
    B myB;
    String name = "A";

    public A(int num) {
        this.name += num;
    }

    public void setMyB(B b) {
        this.myB = b;
    }

    public B getMyB() {
        return this.myB;
    }

    public String getName() {
        return this.name;
    }
}

public class B {
    A myA;

    String name = "B";
    public B(int num) {
        this.name += num;
        myA = new A(num);
    }

    public A getMyA() {
        return this.myA;
    }

    public String getName() {
        return this.name;
    }
}

In this example, we provide an identifier for the class using a defined String and then append a number to it, just to show some unique ID. 在此示例中,我们使用定义的String提供类的标识符,然后在其后附加一个数字,以显示一些唯一的ID。 We provide methods that allow us to access both A and B references, but only B creates its reference to the other via the constructor (composition). 我们提供了允许我们访问AB引用的方法,但是只有B通过构造函数(组成)创建对另一个的引用。

Using this simple test: 使用这个简单的测试:

public class Test {
    public static void main(String[] args) {
        A myA = new A(1);
        B myB = new B(2);

        B anotherB = new B(3);

        myA.setMyB(anotherB);

        System.out.println("A = " + myA.getName());
        System.out.println("A's B = " + myA.getMyB().getName());

        System.out.println("B = " + myB.getName());
        System.out.println("B'a A = " + myB.getMyA().getName());
    }
}

We can see the following output 我们可以看到以下输出

A = A1
A's B = B3
B = B2
B'a A = A2

In this example, the B reference in A is created outside the context of A and passed in as an argument. 在这个例子中,在B基准A被的上下文之外创建A并通过在作为参数。 If we deleted myB , we'd lose the reference to its A , but not if we deleted myA (we still have anotherB . 如果删除myB ,则将丢失对其A的引用,但如果删除myA ,则不会myA (我们仍然还有anotherB

Suppose we eliminated the setMyB() method from A and moved it to the constructor... we'd have an infinite loop of new objects and you'd end up with a StackOverflowError 假设我们从A消除了setMyB()方法,并将其移到构造函数中……我们将有一个无限循环的新对象,而您最终会遇到一个StackOverflowError

You could probably get creative and try to implement the Singleton pattern or some other construct that limits the number of objects created, but doing so would mean the constructors would need to be private/hidden, which prevents extension by other classes. 您可能会很有创造力,并尝试实现Singleton模式或其他限制创建对象数量的构造,但这样做将意味着构造函数将需要为私有/隐藏的,这将阻止其他类的扩展。 Use of a static field to track number of creations might prevent the error, but then you'd lose all the references without having a place to keep track of them all, and lastly, you'd never have a "perfect" composition, because one class would be missing its component. 使用静态字段来跟踪创建的数量可能会避免该错误,但是随后您将丢失所有引用,而又没有一个地方来跟踪所有引用,最后,您将永远不会拥有“完美”的组合,因为一类将丢失其组成部分。

After all this "analysis" you'd end up coming up with a design that makes sense, not one that strictly fits what's drawn on a UML diagram. 经过所有这些“分析”,您最终会得出一种有意义的设计,而不是严格符合UML图上绘制的设计。 The UML diagram is there to convey "relationships" between classes. UML图在那里传达了类之间的“关系”。 The "unique" case you've asked about here where A uses B and B uses A is probably not going to be solved with UML modeling, but probably needs some other design work. 您在此处询问的“独特”案例可能不会用UML建模解决A在哪里使用B和B在哪里使用A,但是可能需要其他一些设计工作。

Q2: What about other relationship in the class diagram, like aggregation, dependence and association? 问题2:关于类图中的其他关系,例如聚合,依赖关系和关联,该怎么办? What's the meaning of cycles in these relationships? 这些关系中的循环是什么意思? Can anyone give some examples? 谁能举一些例子?

Association relationships are really used to describe the type of relationships that are defined by composition, aggregation, many-to-many, one-to-one etc, and depend on the context. 关联关系实际上用于描述由组成,聚合,多对多,一对一等定义的并且取决于上下文的关系类型。 The meaning of cycles in every association is going to depend on your design. 每个关联中循环的含义将取决于您的设计。

In general, a cycle in a dependency means that the class depends on itself. 通常,依赖项中的循环意味着类依赖于自身。 This could be for recursive function calling, Singleton design pattern implementation, or some other design pattern requiring a class to refer to itself. 这可能用于递归函数调用,Singleton设计模式实现或某些其他需要类引用自身的设计模式。

Aggregation was sort of already answered above. 上面已经回答了聚合问题。 It basically means the object "uses" whatever it's aggregating. 从根本上讲,它意味着对象“使用”了所有正在聚合的对象。 An example is a Company aggregates People . 一个示例是Company聚合People When the company goes away, the people still exist. 当公司消失时,人们仍然存在。 A cycle in that kind of relationship is similar to what was shown in my example, except you'd have external references to both A and B classes that were passed as arguments to the first two references to A and B . 这种关系的循环类似于我的示例中所示的循环,不同之处在于,您具有对AB类的外部引用,并将它们作为参数传递给对AB的前两个引用。

The bottom line is... UML is a tool to show relationships between classes. 最重要的是... UML是显示类之间关系的工具。 The design and implementation will follow from that and the fact that you have "interesting" relationships modeled with UML will not help you get past serious design roadblocks. 设计和实现将随之而来,而您拥有使用UML建模的“有趣”关系这一事实并不会帮助您克服严重的设计障碍。

Hopefully this helps shed some light on your questions. 希望这有助于阐明您的问题。

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

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