zoukankan      html  css  js  c++  java
  • Association,Aggregation,Composition概念


    关联
    Association represents the ability of one instance to send a message to another instance. This is typically implemented with a pointer or reference instance variable, although it might also be implemented as a method argument, or the creation of a local variable.

    [Example:]

    |A|----------->|B|

    class A
    {
    private:B* itsB;
    };

    聚合
    Aggregation [...] is the typical whole/part relationship. This is exactly the same as an association with the exception that instances cannot have cyclic aggregation relationships (i.e. a part cannot contain its whole).
    [Example:]

    |Node|<>-------->|Node|

    class Node
    {
    private:vector<Node*> itsNodes;
    };
    The fact that this is aggregation means that the instances of Node cannot form a cycle. Thus, this is a Tree of Nodes not a graph of Nodes.

    Composition [...] is exactly like Aggregation except that the lifetime of the 'part' is controlled by the 'whole'. This control may be direct or transitive. That is, the 'whole' may take direct responsibility for creating or destroying the 'part', or it may accept an already created part, and later pass it on to some other whole that assumes responsibility for it.

    [Example:]

    |Car|<#>-------->|Carburetor|
    |汽车|<#>------->|汽化器|
    class Car
    {
    public:virtual ~Car() {delete itsCarb;}
    private:Carburetor* itsCarb
    };

    聚集是关联中的一种,聚集对象由部分对象组成;
    组合又是一种特殊的聚集。

    在一个组合对象中,部分对象只能作为组成对象的一部分与组合对象同时存在。即是说,组合是“当聚集对象和它的组成对象之间是具有强关联的一种特殊聚集”,组合对象的关键特征是部分对象只能存在于组合对象之中,并且部分体的寿命可能比组合体短,但组合体消亡,部分体也必然消亡。

    我们举例来说明:聚集电脑可以由显示器、CPU、主板、硬盘、键盘、鼠标等聚集而成。在这种关系里面,各个组成部分是可以分拆开独立存在的。

    组合衬衣是由主体、衣领、袖口、衣袖、钮扣等组合而成。在这种关系里面,衣袖或者衣领等如果拆分开来并不能算是一个独立的主体,不具有价值了。

    树是由树干、树根、树枝、树叶等组合而成的。这里面树叶可以先于树消亡,但如果树被砍掉,那么树叶也没有存在价值了。

    多数参考资料上都是上面叙述的那样解释的,不过我觉得理解起来还是有些难度:电脑我把他砸了,电脑坏了,那你说各个组成部分到底是一起消亡了还是没消亡呢?也许应该说至少没有被砸坏的部分还是可以独立使用的;可若这样说的话,那我衬衣破了个洞,我要把衬衣裁减为各个部分,那其他部分是不是也可以拿去拼装为

    我的理解:“把衬衣裁减为各个部分”,这各个部分便是没有定义的部分,不像电脑里的主板硬盘是已经定义的对象,拆了可以作为对象。(如果衬衣裁了成有定义的部分,便可以看作聚合?这样理解对否?)


    Keeping it Simple,
    Aggregation or Composition depends up on life time of the child object.
    聚合和组合决定于它的子对象的生命周期?

    If the child object cannot exist beyond the lifetime of its parent then the relationship is composite(Strong relationship)
    强关系,组合:如果在父生命周期下子对象不能存在,这种关系是组合。

    If the child object can exist beyond the lifetime of its parent, then the relationship is aggregtion.

    However, at a particular moment the child object can be controlled only by a single parent(ie. Unshared), and it is the responsibility of the parent object to transfer the control of the child object to some other instance while its destruction.

    又一论述:
    Associations indicate that the two classes have a relationship as discussed above. 

    Composition and aggregation indicate something more about the relationship:

    Composition indicates that one class belongs to the other.  A polygon is made up of several points.  If the polygon is destroyed, so are the points.

    Aggregation is similar to composition, but is a less rigorous way of grouping things.  An order is made up of several products, but a product continues to exist even if the order is destroyed.

  • 相关阅读:
    invalid expression: missing ) after argument list in xxx 或者 console.error(("[Vue warn]: " + msg + trace));
    js的alert()
    第9节列表渲染
    第8节条件渲染
    第7节class与style绑定
    CF1215D Ticket Game 博弈论
    CF833A The Meaningless Game 思维
    蚯蚓 队列
    洛谷P2566[SCOI2009]围豆豆
    ants 思维
  • 原文地址:https://www.cnblogs.com/yansc/p/635070.html
Copyright © 2011-2022 走看看