65.9K
CodeProject is changing. Read more.
Home

Implementation of Type of Association

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (2 votes)

May 23, 2013

CPOL

2 min read

viewsIcon

25020

downloadIcon

197

Unidirectional, Bidirectional, Aggregation and Composition implementation

Introduction

This post discusses association and its types as listed below:

  1. Unidirectional
  2. Bidirectional
  3. Aggregation
  4. Composition

Using the Code

Association is a relationship which describes the reasons for the relationship and the rules that govern the relationship.

Let’s take an example of Teacher and Student.

Unidirectional Association is a specialized form of association where one object is associated with another object, but the reverse is not true. It is like one way communication.

Let’s take examples of multiple students can associate with a single teacher.

public class Teacher
{
    public string aTeacher = string.Empty;

    public Teacher(string teacherName)
    {
        aTeacher = teacherName;
    }
    public void Associaton(IList<Student> students)
    {
        Console.WriteLine("Following are the students");
        foreach (var student in students)
        {
            Console.WriteLine(student.aStudent);
        }
        Console.WriteLine("associated with a teacher " + aTeacher);
        Console.WriteLine();
    } 
}

Bidirectional Association is a type of association where one object is related with other objects and the reverse is also true. It is like two way communication.

Let’s take examples of multiple students can associate with a single teacher and a single student can associate with multiple teachers.

public class Teacher
{
    public string aTeacher = string.Empty;

    public Teacher(string teacherName)
    {
        aTeacher = teacherName;
    }
    public void Associaton(IList<Student> students)
    {
        Console.WriteLine("Following are the students");
        foreach (var student in students)
        {
            Console.WriteLine(student.aStudent);
        }
        Console.WriteLine("associated with a teacher " + aTeacher);
        Console.WriteLine();
    } 
}

public class Student
{
    public string aStudent = string.Empty;

    public Student(string studentName)
    {
        aStudent = studentName;
    }
    public void Associaton(IList<Teacher> teachers)
    {
        Console.WriteLine("Following are the teachers");
        foreach (var teacher in teachers)
        {
            Console.WriteLine(teacher.aTeacher);
        }
        Console.WriteLine("associated with a student " + aStudent+".");
        Console.WriteLine();
    }
}

But there is no ownership between the objects and both have their own life cycle. Both can create and delete independently.

Aggregation is a specialized form of Association where all objects have their own life cycle but there is ownership and child object cannot belong to another parent object.

If object ‘a’ related with object b and relationship rule stated that object ‘a’ is taking a more important role in the relationship or it is the owner of the relationship, then the association is called as aggregation. Let’s take examples of:

  1. Department and teacher. A single teacher cannot belong to multiple departments, but if we delete the department, teacher object will not destroy.
  2. public class BiologyDempartment
    {
        private IList<Teacher> teachers;
        public BiologyDempartment(IList<Teacher> teachers)
        {
            this.teachers = teachers;
        }
    }
    public class Teacher
    {
        string teacherName;
        public Teacher(string teacherName)
        {
            this.teacherName = teacherName;
        }
    } 
  3. A car object is an aggregation of engine, seat, wheels and other objects.
  4. public class Car
    {
        private Wheel wheel;
        private Engine engine;
    
        public Car(Wheel wheel,Engine engine)
        {
            this.wheel = wheel;
            this.engine = engine;
        }
    } 
    public class Wheel
    { 
    }
    
    public class Engine
    { 
    }
  5. Manager and worker. A manager has workers who work under him.
  6. But if we fire the manager, still the workers are working in the organization, i.e. more happily. Let's keep this as homework.

We can think about “has-a” relationship.

Composition is again a specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. A child object does not have its life cycle and if parent object deletes, all child objects will also be deleted.

Let’s take again an example of relationships.

  1. Between House and rooms. House can contain multiple rooms. There is no independent life of room and any room cannot belong to two different houses if we delete the house room will automatically delete. Each class referenced is considered to be part-of the aggregate class.
  2. public class House
    {
        IList<Room> rooms = new List<Room>();
    }
    
    public class Room
    {
        
    } 
  3. Between questions and options. Single questions can have multiple options and an option cannot belong to multiple questions. If we delete questions, options will automatically delete.
  4. Between class and student. A class contains students and students cannot have multiple classes. A student cannot exist without a class. There exists composition between class and students.
  5. public class ClassRoom
    {
        IList<Student> studets = new List<Student>();
    }
    
    public class Student
    { 
    
    }