blah blah blah is here! blah blah » Close

up0down
link

Hi,

Can you give some practical example of association, aggregation and composition which will make this understandable.
A code sample wud be really helpfull.

regards,

last answered one year ago

2 answers

up0down
link

There's an understandable explanation of these concepts in this article together with some simple C++ examples for all three.

Even if you don't know C++, you'll probably be able to understand these examples.

However, I've translated the first one to C# - I haven't time to translate the others right now *. Although it's unnecessary to manually delete objects in C# (the GC does it for us), I've added a Dispose() method to the classes so as to get the same output as the C++ code.

// association example

using System;


class Department : IDisposable
{
string name_p;

public Department(string dName)
{
Console.WriteLine("Department::Constructor\n");
name_p = dName;
}

public string dName()
{
return name_p;
}

public void Dispose()
{
Console.WriteLine("Department::Dispose\n");
}

}

class Student :IDisposable
{
string name_p;

public Student(string sName)
{
Console.WriteLine("Student::Constructor\n");
name_p = sName;
}

public string sName()
{
return name_p;
}

public void Dispose()
{
Console.WriteLine("Student::Dispose\n");
}
}


class Course : IDisposable
{
Student std_p;
Department dept_p;
string courseName_p;

static int index = 0;
static Course[] courseList = new Course[4];

public Course(string crseName, Student student, Department dept)
{
courseName_p = null;
std_p = student;
dept_p = dept;
Console.WriteLine("Course::Constructor\n");

if (index < 4)
{
courseName_p = crseName;

//insert this Course in courseList
courseList[index] = this;
++index;
}
else
{
Console.WriteLine("Cannot accomodate any more Courses\n");
}
}

public void Dispose()
{
Console.WriteLine("Course::Dispose\n");
}

public static string findStudent(string crseName, string deptName)
{
for(int i=0; i<index; i++)
{
if ( (courseList[i].CourseName == crseName) &&
(courseList[i].DeptName == deptName) )
{
return courseList[i].StdName;
}
}
return "";
}

public string StdName{ get { return std_p.sName(); } }
public string DeptName { get { return dept_p.dName(); } }
public string CourseName { get{ return courseName_p; } }
}

static class Program
{

static void Main()
{
int i;

Console.WriteLine("\nExample of Association class...\n");
Console.WriteLine("-----------------------------------\n\n");

Console.WriteLine("We have got 4 students ...\n");
Student[] studentNames = new Student[4]{new Student("Meera"), new Student("Moina"), new Student("Teena"), new Student("Mridula")} ;

Console.WriteLine("\n");

Console.WriteLine("We have got 2 Departments...\n");
Department[] departNames = new Department[2]{new Department("Mathemetics"), new Department("ComputerScience")} ;

Console.WriteLine("\n");

Console.WriteLine("Here class Course Associates Student and Department, with a Course name ...\n");
Course course1 = new Course("DataStructure",studentNames[0], departNames[1]);
Course course2 = new Course("Maths",studentNames[3], departNames[0]);
Course course3 = new Course("Geometry",studentNames[2], departNames[0]);
Course course4 = new Course("CA",studentNames[1], departNames[1]);

Console.WriteLine("\n");

Console.WriteLine("Finding a Student using Course and Department...\n");
Console.WriteLine("Student who has taken Maths Course in Mathemetics Department is : {0}", Course.findStudent("Maths", "Mathemetics"));

Console.WriteLine("\n");

Console.WriteLine("Disposal of objects...\n\n");

for(i=0; i<4; ++i)
{
studentNames[i].Dispose();
}

Console.WriteLine("\n");

for(i=0; i<2; ++i)
{
departNames[i].Dispose();
}

Console.WriteLine("\n");

course1.Dispose();
course2.Dispose();
course3.Dispose();
course4.Dispose();

Console.WriteLine("\n");

Console.ReadKey();
}
}


EDIT

* I've found time now to translate the other two examples. Not quite the same because of the different object disposal semantics between C++ and C#:
// aggregation example

using System;

class Employee : IDisposable
{
public Employee(string name)
{
Console.WriteLine("Employee::Constructor\n");
myName_p = name;
}

public string disp(){ return myName_p ;}

public void Dispose()
{
Console.WriteLine("Employee::Dispose\n");
}

private string myName_p;
}

class Company : IDisposable
{

public Company(string compName, Employee emp)
{
Console.WriteLine("Company::Constructor\n");
name = compName;
myEmp_p = emp;
}

public void Dispose()
{
Console.WriteLine("Company::Dispose\n");
myEmp_p.Dispose();
myEmp_p = null;
}

private string name;
private Employee myEmp_p;
}

static class Program
{
static void Main()
{
Console.WriteLine("\nExample of Aggregation Relationship\n");
Console.WriteLine("-----------------------------------------\n\n");

Console.WriteLine("Here, an Employee-Keerti works for Company-MS \n");
Employee emp = new Employee("Keerti");


Company comp = new Company("MS", emp);
// here Company object and Employee objects will be disposed
Console.WriteLine("At this point Company gets disposed...\n");
Console.WriteLine("and so does Employee-" + emp.disp() + "\n");
comp.Dispose();
Console.ReadKey();
}
}

// composition example

using System;

class Room : IDisposable
{
public static void createRoom_v(out Room room, House hse, string name)
{
room = new Room(hse, name);
}

public Room(){}

public Room(House hse, string myName)
{
Console.WriteLine("Room::Constructor\n");
myHse_p = hse;

if(myHse_p != null)
{
name_p = myName;
}
else
{
Console.WriteLine("Oops House itself is not Created Yet ...\n");
}
}

public void Dispose()
{
Console.WriteLine("Room::Dispose\n");
myHse_p = null;
}

public void disp()
{
Console.WriteLine(name_p);
Console.WriteLine("\n");
}

public static void initList_v(out Room[] roomsList_p)
{
roomsList_p = new Room[3];
}

private House myHse_p;
private string name_p;
}

class House : IDisposable
{
public House(string myName)
{
Console.WriteLine("House::Constructor\n");
name_p = myName;

Room.initList_v(out roomsList_p);

Room myRoom;
Room.createRoom_v(out myRoom, this, "Kitchen");
roomsList_p[0] = myRoom;

Room.createRoom_v(out myRoom, this, "BedRoom");
roomsList_p[1] = myRoom;

Room.createRoom_v(out myRoom, this, "Drawing Room");
roomsList_p[2] = myRoom;
}

public void Dispose()
{
Console.WriteLine("House::Dispose\n");
int i;

Console.WriteLine("Dispose all the Rooms ...\n");
for(i=0; i<3; ++i)
{
if(roomsList_p[i] != null)
{
roomsList_p[i].Dispose();
roomsList_p[i] = null;
}

}
roomsList_p = null;
}

public void disp()
{
Console.WriteLine("\n\nName of the House : " + name_p);

if(roomsList_p != null)
{
int i;
Console.WriteLine("\n\nRooms details...\n");
for(i=0; i<3; ++i)
{
if(roomsList_p[i] != null)
{
roomsList_p[i].disp();
}
}
Console.WriteLine("\n\n");
}
}

private string name_p;
private Room[] roomsList_p;
}


static class Program
{
static void Main()
{
Console.WriteLine("\nExample of Composition Relationship\n");
Console.WriteLine("-----------------------------------------\n\n");
House hse = new House("Vishranti Nilaya");

Console.WriteLine("\n\nHouse details...\n");
hse.disp();

Console.WriteLine("Here House itself creates the Rooms and disposes them as well, before it gets disposed...\n");
hse.Dispose();
Console.ReadKey();
}
}

up0down
link

Thanks !!! the artical was good and thanks a lot for taking the effort to convert the code.....Really appreciate your time...hope i will be able to do the same sometime for others !!! :-)

Feedback