zoukankan      html  css  js  c++  java
  • what is the difference between Association, Dependence, Aggregation and Composition

    compare to the association and dependence ,the aggregation and composition relationship of two class is closer。

    let 's check it out.

    This short article will put forward my understanding of composition and aggregation and how I would express it in C# code. 

    Composition:

    As we know, inheritance gives us an 'is-a' relationship. To make the understanding of composition easier, we can say that composition gives us a 'part-of' relationship. Composition is shown on a UML diagram as a filled diamond (see Figure 1). 

     

    If we were going to model a car, it would make sense to say that an engine is part-of a car. Within composition, the lifetime of the part (Engine) is managed by the whole (Car), in other words, when Car is destroyed, Engine is destroyed along with it. So how do we express this in C#?

    public class Engine
    {
     . . . 
    }

    public class Car

    {

        Engine e = new Engine();

        .......

    }

    As you can see in the example code above, Car manages the lifetime of Engine.

    Aggregation:

    If inheritance gives us 'is-a' and composition gives us 'part-of', we could argue that aggregation gives us a 'has-a' relationship. Within aggregation, the lifetime of the part is not managed by the whole. To make this clearer, we need an example. For the past 12+ months I have been involved with the implementation of a CRM system, so I am going to use part of this as an example.

    The CRM system has a database of customers and a separate database that holds all addresses within a geographic area. Aggregation would make sense in this situation, as a Customer 'has-a' Address. It wouldn't make sense to say that an Address is 'part-of' the Customer, because it isn't. Consider it this way, if the customer ceases to exist, does the address? I would argue that it does not cease to exist. Aggregation is shown on a UML diagram as an unfilled diamond (see Figure 2).  

     

    So how do we express the concept of aggregation in C#? Well, it's a little different to composition. Consider the following code:

    public class Address
    {
     . . .
    }

    public class Person

    {

         private Address address;

         public Person(Address address)

         {

             this.address = address;

         }

         . . .

    }

    Person would then be used as follows:

    Address address = new Address();
    Person person = new Person(address);

    or

    Person person = new Person( new Address() );

    As you can see, Person does not manage the lifetime of Address. If Person is destroyed, the Address still exists. This scenario does map quite nicely to the real world.

    Conclusion:

    As I said at the beginning of the article, this is my take on composition and aggregation. Making the decision on whether to use composition or aggregation should not be a tricky. When object modelling, it should be a matter of saying is this 'part-of' or does it 'have-a'? 

    Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation. 


    Dependency means Change in structure or behaviour of a class affects the other related class, then there is a dependency between those two classes. It need not be the same vice-versa. When one class contains the other class it this happens. 

     

  • 相关阅读:
    [CSP-S模拟测试]:影子(并查集+LCA)
    [CSP-S模拟测试]:夜鹰与玫瑰(数学)
    [CSP-S模拟测试]:抛硬币(DP)
    [CSP-S模拟测试]:影魔(树状数组+线段树合并)
    [CSP-S模拟测试]:队长快跑(DP+离散化+线段树)
    [CSP-S模拟测试]:玄学题/c(数学)
    [CSP-S模拟测试]:卡常题/b(基环树+DP)
    [CSP-S模拟测试]:工业题/a(数学)
    BZOJ5297 [Cqoi2018]社交网络 【矩阵树定理】
    BZOJ5300 [Cqoi2018]九连环 【dp + 高精】
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2513618.html
Copyright © 2011-2022 走看看