关联、组合和聚合 - 用 java 实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13570241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:15:23  来源:igfitidea点击:

Association, Composition and Aggregation - Implementation with java

javaassociationsumlcompositionaggregation

提问by sharon Hwk

I'm a bit confused about Association, Aggregation and Composition. Even though loads of website, and forums discusses this topic, i have become more confused after reading some of them.

我有点困惑Association, Aggregation and Composition。尽管大量的网站和论坛讨论了这个话题,但在阅读了其中的一些之后,我变得更加困惑。

I want to know if the following are correct :

我想知道以下是否正确:

1.) Aggregation - WIll exist if the Whole is destroyed. for example an Enginecould exist with or without a Car.

1.) 聚合——如果整体被摧毁,就会存在。例如 anEngine可以存在或不存在Car

2.) Composition - Will not exist if the object is destroyed. for example a Roomcan't exist without a House.

2.) 组合 - 如果对象被销毁,将不存在。例如,Room没有a就不能存在House

3.) Association - I am not sure, at what instances we should use this. Can someone comment on this.

3.) 关联 - 我不确定,在什么情况下我们应该使用它。有人可以对此发表评论。

When it comes to writing Java codes for Aggregation, COmposition and Association

在编写用于聚合、组合和关联的 Java 代码时

4.) Aggregation

4.)聚合

Class Car {

    private Engine engine;

    public void setEngine(Engine engine){
     this.engine=engine;
    }  

    public Engine getEngine(){
     return engine;
    }
}

I think that if it's an Aggregation association, then there should be accessors and mutatorsdefined. Is this correct ?

我认为如果它是一个聚合关联,那么应该accessors and mutators定义。这个对吗 ?

According to my definition of Aggregation, i mentioned that if the Carobject is destroyed, the Enginecan be accessed. But, i don't see that hapenning in the above code. If Car object is destroyed, there will not be any way to access the Engine like car.getEngine(). So how does this happen ?

根据我对聚合的定义,我提到如果Car对象被破坏,则Engine可以访问。但是,我在上面的代码中没有看到这种情况。如果 Car 对象被销毁,将无法像car.getEngine(). 那么这是怎么发生的呢?

Composition

作品

Public House {
   private Room room;
   Public House (){
      room = new Room (int noRooms, String nameOfHouse);

   }
}

I think that if it's a C0mposition, then there should not be any accessor mutatorfunctions. and the Object Roomhas to be created only inside the Constructor. and the instance method that holds the roomobject must be Private? Am i correct ?

我认为如果它是 a C0mposition,那么不应该有任何accessor mutator功能。并且对象Room必须仅在构造函数内部创建。并且持有room对象的实例方法必须是Private?我对么 ?

note: Once again, i have googled, and visited many websites/forums trying to clear my doubts.Similar questions were posted in many forums, but yet i only got more confused about this. Can someone help me clear my doubts. Thank you!

注意:我再次使用谷歌搜索并访问了许多网站/论坛,试图消除我的疑虑。许多论坛上都发布了类似的问题,但我对此感到更加困惑。谁能帮我解开疑惑。谢谢!

回答by JB Nizet

The type of the association has little impact on how you implement it. The fact that it's an aggregation or a composition is only a semantic difference.

关联的类型对您如何实现它几乎没有影响。它是聚合或组合这一事实只是语义上的差异。

The difference appears if the objects are persisted in some database. In this case, if the container object is removed from the database, the contained object is also removed or not, depending on the type of the association.

如果对象保留在某个数据库中,则会出现差异。在这种情况下,如果容器对象从数据库中删除,则所包含的对象也会被删除,这取决于关联的类型。

回答by Vishal

To define aggregation association we take another example.... A person and a company...

为了定义聚合关联,我们再举一个例子......一个人和一个公司......

A person and a company are two individual entities, but a person can work in a company and he is called as an employee... So employee has an existence in outer world as an person. So even if company perishes, but person will not perish...

一个人和一个公司是两个独立的实体,但是一个人可以在一个公司工作,他被称为雇员……所以雇员作为一个人存在于外部世界。所以即使公司灭亡,人也不会灭亡……

    public class Example {

    public static void main(String[] args) {
        Person p1 = new Person("Vishal");
        Company abc = new Company(p1);
    }


}

class Person{
    String name;
    public Person(String name) {
        this.name = name;
    }
}

class Company{
    Person employee;
    public Company(Person p1) {
        employee = p1;
    }
}

I did not go into the actual definitions of the company and person, but I think it will sense now.

我没有深入了解公司和个人的实际定义,但我认为现在它会有意义。

Whereas composition association is an association between a company and a department. As soon as company perish, it's department does not make any sense, so it must perish...

而组合关联是公司和部门之间的关联。公司一灭,它的部门就没有任何意义,所以它必须灭亡……

Another example of composition association is order and different items of the order. You order few books(3) from an online store. In this case 3 order lines(items) must be cancelled if order is cancelled. So existence of order lines depends only on order.

组合关联的另一个示例是订单和订单的不同项目。您从网上商店订购了几本书 (3)。在这种情况下,如果取消订单,则必须取消 3 个订单行(项目)。因此订单行的存在仅取决于订单。

Manager uses a swipe card to enter XYZ premises. Manager object and the swipe card object use each other but they have their own object life time. In other words, they can exist without each other. The most important point in this relationship is that there is no single owner.

经理使用刷卡进入 XYZ 场所。管理器对象和刷卡对象相互使用,但它们有自己的对象生命周期。换句话说,它们可以在没有彼此的情况下存在。这种关系中最重要的一点是没有单一的所有者。

Whereas in aggregation one object owns another object...

而在聚合中,一个对象拥有另一个对象......

It is a nice link on these 3 concepts... For association you can check 2nd requirement... http://www.codeproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit

这是关于这 3 个概念的一个很好的链接...对于关联,您可以检查第二个要求... http://www.codeproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit

回答by jsky

Further to Vishal's answer, you could have an Employee class extending Person class, so that Company holds an Employee instead of a Person and when the company is destroyed, the Employee goes with it (collected as garbage) and not a Person. This is just to be more analagous to reality and may not be necessary.

继 Vishal 的回答之后,您可以有一个 Employee 类扩展 Person 类,以便 Company 持有 Employee 而不是 Person,并且当公司被销毁时,Employee 会随之而来(作为垃圾收集)而不是 Person。这只是为了更贴近现实,可能没有必要。

回答by UML GURU

It is a difficult question to cover all possible java codes for associations. The best would be to download EclipseUML Omondo which is a kind of Java to UML bible. This tool is only for UML to Java mapping. No MDD but live synchronization. Yoiu can look what code you get from your class diagram.

涵盖所有可能的关联 java 代码是一个困难的问题。最好的方法是下载 EclipseUML Omondo,这是一种 Java 到 UML 的圣经。此工具仅用于 UML 到 Java 的映射。没有 MDD,而是实时同步。你可以看看你从你的类图中得到了什么代码。

The code is live synchronized from the graphical class diagram therefore you can play and redo as many times as you want in order to cover all possible java codes.

代码是从图形类图中实时同步的,因此您可以根据需要多次播放和重做,以涵盖所有可能的 Java 代码。

There is still a 30 days evaluation copy which let you enough time to see what should be the java code even if the newest releases are only reserved to paid customer.

仍然有 30 天的评估副本,即使最新版本只保留给付费客户,也可以让您有足够的时间查看 Java 代码应该是什么。