What is the difference between Composition and Association relationship?

前端 未结 8 1938
眼角桃花
眼角桃花 2020-12-01 04:05

In OOP, what is the difference between composition (denoted by filled diamond in UML) and association (denoted by empty diamond in UML) relationship between classes. I\'m a

相关标签:
8条回答
  • 2020-12-01 04:31

    Composition is a stricter relationship than aggregation. Composition means that something is so strongly related to something else that they cannot basically exist independently, or if they can, they live in different contexts.

    Real world example: you define a GUI window, and then a text field where to write something. Between the class defining the GUI and the class defining the text field there's composition. Together, they compose a widget which can be seen as an entity on its own. Suppose you delete the window, and you delete the text field as well.

    Aggregation is different, in the sense that the link between the two entities is temporary, unstable, and occasional. A real world example. Suppose you have a database of objects containing multiple data instances. Now you run some filter to collect the data instances obeying a given criterium, and the resulting instances are pushed into a graphical list so that the user can see them. When the graphical widget receives the objects, it can form an aggregation of these entities, and present them. If the user closes the window with the graphical list, and the latter get deleted, the data objects should not be deleted. Maybe they are displayed somewhere else, or you still need them.

    Also, in general, composition is defined at creation time. Aggregation is instead defined later in the object lifetime.

    0 讨论(0)
  • 2020-12-01 04:35

    I believe that a code-based example can help to illustrate the concepts given by the above responses.

    import java.util.ArrayList;
    
    public final class AssoCia
    {
        public static void main( String args[] )
        {
            B b = new B();
            ArrayList<C> cs = new ArrayList();
    
            A a = new A( b, cs );
    
            a.addC( new C() );
            a.addC( new C() );
            a.addC( new C() );
            a.listC();
        }
    }
    
    class A
    {
        // Association -
        // this instance has a object of other class
        // as a member of the class.
        private B b;
    
        // Association/Aggregation -
        // this instance has a collection of objects
        // of other class and this collection is a
        // member of this class
        private ArrayList<C> cs;
    
        private D d;
    
        public A(B b, ArrayList<C> cs)
        {
            // Association
            this.b = b;
    
            // Association/Aggregation
            this.cs = cs;
    
            // Association/Composition -
            // this instance is responsible for creating
            // the instance of the object of the
            // other class. Therefore, when this instance 
            // is liberated from the memory, the object of
            // the other class is liberated, too.
            this.d = new D();
        }
    
        // Dependency -
        // only this method needs the object
        // of the other class.
        public void addC( C c )
        {
            cs.add( c );
        }
    
        public void listC()
        {
            for ( C c : cs )
            {
                System.out.println( c );
            }
        }
    
    }
    
    class B {}
    
    class C {}
    
    class D {}
    
    0 讨论(0)
提交回复
热议问题