Can you give some practical example of association, aggregation and composition which will make this understandable. A code sample wud be really helpfull.
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 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"));
* 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;
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("Here House itself creates the Rooms and disposes them as well, before it gets disposed...\n"); hse.Dispose(); Console.ReadKey(); } }
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 !!! :-)
2 answers
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.
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#:
answered one year ago by:
17279
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 !!! :-)
answered one year ago by:
0