Categories
PHP

OOP Composition and Aggregation

In the composition design pattern an object creates another object. Aggregation occurs when an object is composed of multiple objects.

Relationships between objects.

Composition is an important concept in PHP. In this design pattern, an object creates another object. Aggregation occurs when an object is composed of multiple objects.

Composition is effectively an ownership relationship, while aggregation is a “contains” relationship. In composition, the parts can not exist outside the thing that contains them, but individual things can exist on their own as unique entities in aggregation.

Composition example

We will create two classes, Author and Books. In the Author class we’ll store author information such as author name, email address etc:

class Author {
 private $name;
 private $email;

 public function __construct($name,$email){
  $this->name = $name;
  $this->email = $email;
 }

 public function getName(){
  return $this->name;
 }

 public function getEmail(){
  return $this->email;
 }
}

In the Book class we will store information related to a book, such as book name, price and author information (Author object).

To add an author for the book, we’ll use the addAuthor method in the Book class. This method creates an Author object and store it in the $authors array (a book can have multiple authors).

<?php
 class Book {
  //store Author object
  private $authors = array();
  private $price;
  private $name;

  public function __construct($name,$price){
   $this->name = $name;
   $this->price = $price;
  }

  public function setPrice($price){
   $this->price = $price;
  }

  public function getPrice(){
   return $this->price;
  }

  public function getName(){
   return $this->name;
  }

  public function addAuthor($name,$email){
   $this->authors[] = new Author($name,$email);
  }

  public function getAuthors(){
   return $this->authors;
  }
}

Let’s create a new book named “My PHP Book” and set it price then add two authors using the Book::addAuthor method:

<?php
 $book = new Book('My PHP Book',10.0);
 $book -> addAuthor('Kelly','kelly@brainbell.com');
 $book -> addAuthor('Ken','ken@brainbell.com');

 # Print book info
 echo $book -> getName();
 echo $book -> getPrice();

 # Print book's authors info
 $authors = $book -> getAuthors(); //Array of authors
 foreach ($authors as $author) {
  echo $author -> getName();
  echo $author -> getEmail();
 }

As we discussed earlier that in the composition design pattern an object creates another object. In the above code, the Book object created two Author objects.

Aggregation example

Aggregation is a design pattern that allows one object to act as a container for one or more other objects. Aggregation and composition are similar to association. In our previous example, the Book object was not only creating the Author object it also storing them, so it’s also following the aggregation pattern.

To make the above code to follow only aggregation pattern, alter the addAuthor method in the Book class, instead of creating an Author object in the Book class, create them outside the class and then store them in the Book class:

<?php
 class Book {
  //store Author object
  private $authors = array();
  private $price;
  private $name;

  public function __construct($name,$price){
   $this->name = $name;
   $this->price = $price;
  }

  public function setPrice($price){
   $this->price = $price;
  }

  public function getPrice(){
   return $this->price;
  }

  public function getName(){
   return $this->name;
  }

 /* public function addAuthor($name,$email){
   $this->authors[] = new Author($name,$email);
  } */

  public function addAuthor($author){
   $this->authors[] = $author;
  }

  public function getAuthors(){
   return $this->authors;
  }
}

Next, create the Author object outside the Book class code and then use the addAuthor method to store the Author object in the Book object:

<?php
 $ken = new Author ('Ken','ken@brainbell.com');
 $kelly = new Author ('Kelly','kelly@brainbell.com');

 $book = new Book('My PHP Book',10.0);
 $book->addAuthor($ken);
 $book->addAuthor($kelly);


 # Print book info
 echo $book -> getName();
 echo $book -> getPrice();

 # Print book's authors info
 $authors = $book -> getAuthors(); //Array of authors
 foreach ($authors as $author) {
  echo $author -> getName();
  echo $author -> getEmail();
 }

PHP OOP Tutorials: