Differences in composition and aggregation

Source: Internet
Author: User

We all know that the UML class diagram generally contains five kinds of relations, that is, association aggregation generalization dependency Some people may feel that combination and aggregation is a little difficult to distinguish between the fact that it is relatively different from the other relationships actually want to distinguish between these two relationships is not difficult

Aggregation and composition are all associations, but they are two special associations because this is the same root, so there will be similarities between them. Let's look at how they differ.

The concept of aggregation and composition I'm sure you don't need me to go into this. Here's a direct example.

Cheng Teacher's "big talk" in the case of the wild goose is very appropriate here I borrowed a wild goose like lively afraid of loneliness so they have been living in a social life so there are geese flock every geese have their own geese flock each geese have a lot of geese and geese flock this relationship can be called aggregation In addition, each goose has two wings. The relationship between the wild Goose and the Wild Goose Wings is called the combination has this visible aggregation relationship obviously no combination close the geese will not be able to survive because of their group of geese, and the wild Goose wings can not be separated from the wild Goose and live alone--the class of combinatorial relationships has the same life cycle


Aggregation diagram:


Composition diagram:



The difference between these two types of relationships from the code is:

Constructors of different wild goose groups:

[CSharp] 

        Public  class Goosegroup  
        {public  
            Goose Goose;  
      
      
            Public Goosegroup (Goose Goose)  
            {  
                this.goose = Goose;  
            }  
        }  


Wild Goose Category:
[CSharp]

    public class Goose  
    {public  
        Wings Wings;  
      
        Public Goose ()  
        {  
            wings=new wings ();  
        }  
    }  





The class in the aggregation relationship contains another class as a parameter

Wild Geese (Goose) are used as parameters to pass the values in the Goosegroup (The Wild Goose Class) (Goose) can be separated from the wild Goose class and exist independently.

The class that combines the relationships contains the instantiation of another class

Wild Goose (Goose) before instantiating, be sure to instantiate the Wings Class (Wings) Two classes tightly coupled together they have the same life cycle Wing class (Wings) can not be separated from the Wild Goose Class (Goose) and independent existence

The encapsulation of information is different

In the aggregation relationship, the client can know both the wild Goose and the wild goose, because they are independent

But in the combinatorial relationship, the client only knows the Wild goose class, does not know the existence of the wing class, because the wing class is tightly encapsulated in the Wild Goose class.

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.