10 探索类与类之间的关系(未完待续)

2017-11-08  本文已影响5人  Closears
  1. Object Composition (可细分为CompositionAggregation两部分),
    "it allows us to create complex classes by combining simpler, more easily manageable parts."
    1.1 Composition与Aggregation的共同点
    The part (member) is part of the object (class) . - part-whole relationship
    The part (member) does not know about the existence of the object (class).- unidirectional relationship
    1.2 Composition与Aggregation的区别

Composition:

  1. Parts can belong to only one object at a time.
  1. The whole object is responsible for the existence and lifespan of the parts.
  1. It models "part-of" relationships.
  1. In a composition, we typically add our parts to the composition using normal member variables (or pointers where the allocation and deallocation process is handled by the composition class).

Aggregation:

  1. Parts can belong to more than one object at a time.
  1. The whole object is not responsible for the existence and lifespan of the parts.
  1. It models "has-a" relationships.
  1. In an aggregation, we also add parts as member variables. However, these member variables are typically either references or pointers that are used to point at objects that have been created outside the scope of the class.
  1. TBD
上一篇下一篇

猜你喜欢

热点阅读