Aggregation: the object exists outside the other, is created outside, 开发者_C百科so it is passed as an argument (for example) to the construtor. Ex: People – car. The car is create in a different context and than becomes a person property.
Composition: the object only exists, or only makes sense inside the other, as a part of the other. Ex: People – heart. You don’t create a heart and then pass it to a person.
Does this mean with DI frameworks, aggregation is the preferred and more common approach, and improves testability?
Does this also mean the lack of multiple inheritance in a language like C# can be mitigated through the use of aggregation. I have always heard the discussion framed in terms of "composition versus inheritance". Is there a reason for this?
Delegation provides for a more flexible solution than inheritance - there is a good article called why extends is evil that explains more about this - however which version (whether it be Aggregation or Composition) is academic - you have identified the differences, and which one to use when is down to what you are trying to achieve. Lack of multiple inheritance is usually mitigated through interfaces though.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论