Click here to Skip to main content
Click here to Skip to main content

Tagged as

Implementation of Type of Association

, 24 May 2013
Rate this:
Please Sign up or sign in to vote.
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
    { 
    
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Amey K Bhatkar
Web Developer Lionbridge
India India
Amey K Bhatkar, a “Microsoft .Net” Web Developer.
I am programmer by will and profession.
I have completed my MCA in 2011 and join software industry.
Presently I am working with Lion Bridge Technologies in Mumbai - India
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 4 PinmemberOron Mizrachi23-May-13 7:03 
GeneralRe: My vote of 4 PinprofessionalAmey K Bhatkar25-May-13 8:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 24 May 2013
Article Copyright 2013 by Amey K Bhatkar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid