avatarAnna Azzam

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1763

Abstract

container. If the container is destroyed, the child is also destroyed.</p><p id="5217">Take for example a <code>Page</code> and a <code>Book</code>. The <code>Page</code> cannot exist without the <code>Book</code>, because the book is <b><i>composed of</i></b><i> </i><code>Pages</code>. If the <code>Book</code> is destroyed, the <code>Page</code> is also destroyed.</p><p id="9fca">In code, this usually refers to the child instance being created inside the container class:</p><div id="5a35"><pre><span class="hljs-keyword">class</span> <span class="hljs-title class_">Book</span>: <span class="hljs-keyword">def</span> <span class="hljs-title function_">init</span>(<span class="hljs-params"><span class="hljs-variable language_">self</span></span>): page1 = <span class="hljs-title class_">Page</span>(<span class="hljs-string">'This is content for page 1'</span>) page2 = <span class="hljs-title class_">Page</span>(<span class="hljs-string">'This is content for page 2'</span>) <span class="hljs-variable language_">self</span>.pages = [page1, page2]

<span class="hljs-keyword">class</span> <span class="hljs-title class_">Page</span>: <span class="hljs-keyword">def</span> <span class="hljs-title function_">init</span>(<span class="hljs-params"><span class="hljs-variable language_">self</span>, content</span>): <span class="hljs-variable language_">self</span>.content = content</pre></div><div id="7464"><pre><span class="hljs-attr">book</span> = Book() <span class="hljs-comment"># If I destroy this Book instance,</span> <span class="hljs-comment"># the Page instances are also destroyed</span></pre></div><h2 id="86e6">Aggregation</h2><p id="89e4">With an aggregation, the child <b><i>can

Options

</i></b> exist independently of the parent.</p><p id="b202">So thinking of a <code>Car</code> and an <code>Engine</code>, the <code>Engine</code> doesn’t need to be destroyed when the <code>Car</code> is destroyed.</p><div id="a1d8"><pre><span class="hljs-keyword">class</span> <span class="hljs-title class_">Car</span>: <span class="hljs-keyword">def</span> <span class="hljs-title function_">init</span>(<span class="hljs-params"><span class="hljs-variable language_">self</span>, engine</span>): <span class="hljs-variable language_">self</span>.engine = engine

<span class="hljs-keyword">class</span> <span class="hljs-title class_">Engine</span>: <span class="hljs-keyword">def</span> <span class="hljs-title function_">init</span>(<span class="hljs-params"><span class="hljs-variable language_">self</span></span>): pass</pre></div><div id="a80e"><pre><span class="hljs-attr">engine</span> = Engine() <span class="hljs-attr">car</span> = Car(engine) <span class="hljs-comment"># If I destroy this Car instance,</span> <span class="hljs-comment"># the Engine instance still exists</span></pre></div><h1 id="3b9b">How are these represented a UML diagram?</h1><p id="48f3">In a UML diagram, both Aggregation and Composition are represented with a <b>diamond arrow between the classes</b>. The diamond end goes on the side of the <b><i>container</i></b><i>.</i></p><ul><li>Aggregation uses an <b>open diamond</b></li><li>Composition uses a <b>closed diamond</b></li></ul><p id="d80d">Here’s an example:</p><figure id="2062"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*D7TuM5p9Dl-tEELM3HnySQ.png"><figcaption></figcaption></figure><p id="2dd6"><i>Hope this helped!</i></p></article></body>

Aggregation vs. Composition in Object Oriented Programming

In Object Oriented Programming, there are many different types of relationships which can exist between two or more classes. The most common two types are:

  • Inheritance an “is a” relationship
  • Associationa “has a” relationship

This blog is going to do a deep dive into the two types of Association relationships — Aggregation and Composition.

What is an Association relationship?

An association relationship between two classes is a “has a” relationship. For example:

  • A Car has an Engine and a Wheel
  • A Person has a Leg and an Arm
  • A Book has Pages

This usually represents when there are two classes, ClassA and ClassB, and either:

  • ClassA contains ClassB as an attribute, or
  • Instances of ClassB are constructed inside ClassA

What’s the difference between Aggregation and Composition?

There are two sub-types of Association relationships — Aggregation and Composition. What’s the difference between these two?

Composition

Composition implies that the contained class cannot exist independently of the container. If the container is destroyed, the child is also destroyed.

Take for example a Page and a Book. The Page cannot exist without the Book, because the book is composed of Pages. If the Book is destroyed, the Page is also destroyed.

In code, this usually refers to the child instance being created inside the container class:

class Book:
    def __init__(self):
       page1 = Page('This is content for page 1')
       page2 = Page('This is content for page 2')
       self.pages = [page1, page2]

class Page:
    def __init__(self, content):
        self.content = content
book = Book() # If I destroy this Book instance,
              # the Page instances are also destroyed

Aggregation

With an aggregation, the child can exist independently of the parent.

So thinking of a Car and an Engine, the Engine doesn’t need to be destroyed when the Car is destroyed.

class Car:
    def __init__(self, engine):
        self.engine = engine

class Engine:
    def __init__(self):
        pass
engine = Engine()
car = Car(engine) # If I destroy this Car instance,
                  # the Engine instance still exists

How are these represented a UML diagram?

In a UML diagram, both Aggregation and Composition are represented with a diamond arrow between the classes. The diamond end goes on the side of the container.

  • Aggregation uses an open diamond
  • Composition uses a closed diamond

Here’s an example:

Hope this helped!

Object Oriented
Programming
Design Patterns
Python
Code
Recommended from ReadMedium