Differences and linkages between Java composition and aggregation and the inclusion relationships of collections on the data

Source: Internet
Author: User

contains a collection and a collectionBetweenthe relationship, also calledSubsetrelationships. Basic meaningis near the same as implication, implication, inclusion,Relationshipadjectives.
the inclusions between elements and collections are called elements belonging to the collectionExample a={1,2},b={1,2,3} then 1∈a,2∈a,3∈b belongs to the relationship between the element and the set, for example, element a belongs to collection A, and a∈a belongs to the symbol: ∈, used between the element and the collection the inclusion between the collection and the collection is called the containingIf any of the elements of collection A are elements of set B, then the set a is called a subset of Set B, and a contained in B or B contains an empty set that is contained by any one by one collection, which is a subset of any set if the element of set A is a subset of Set B, and at least one of the elements in B does not belong to a So the set A is called a true subset of Set B, which is recorded as a true inclusion of B or B really contains a.

There is a big difference between composition and aggregation, not in form, but in nature:
For example, a class contains a reference B of Class B, when an object of Class A dies, b the object that the reference points to also dies (no reference to it, it becomes a garbage object), this is called the combination, and the object that B points to has another reference to it, which is called aggregation.

In real life:
Man and hand, the foot is a combination of relations, because when the human death of the hand of the future is no longer exist. The person and his computer are the aggregation relationship.

  1. Class hand{
  2. }
  3. Class computer{
  4. }
  5. Combination:
  6. Class person{
  7. Private Hand Hand;
  8. Public person () {
  9. Hand = new Hand ();
  10. }
  11. }
  12. Polymerization:
  13. Class person{
  14. Private computer computer;
  15. Public Setcomputer () {
  16. Computer = new computer ();
  17. }
  18. }

It can be said that aggregation is a strong combination of relationships

Differences and linkages between composition and aggregation:
First of all, the relationship between the whole and the part, the combination of a stronger relationship, for the combined relationship, if the loss of part, the whole will not exist.
The code implementation looks:
Composition: The part that is instantiated in the overall constructor, which cannot be shared by other instances. The whole and part of the life cycle are synchronized. The part of the aggregation relationship can be initialized in the form of parameter passing in the constructor.
From a database perspective: Combinatorial relationships: cascade deletions are required, and aggregation relationships are not required.


After reading these relationships, we can get a sense of comprehension. In fact, the combinatorial relationship in Java is the inclusion relationship of a mathematical set.

There is a great similarity between the combination and the two meanings

It is easy to understand the statements in some programs with the inclusion of:

A a=new a ();

b b=new B (a);

C c=new C (b);

In the three sections of code, C contains the b,b containing a

If you use a mathematical formula to express

A belongs to B,b belongs to C

This makes it easy to understand what these three words really mean:

Is that all the functions and attributes B in a have b that can be used directly

All functions and attributes C in B are available directly with C

It's like a C is a person B is a hand C is a finger that contains a


Such a layer of the combined relationship with the data is very good understanding of mathematics and profound AH

Differences and linkages between Java composition and aggregation and the inclusion relationships of collections on the data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.