Association, Aggregation & Composition in OOP

Association (एसोसिएशन)

Association OOP में एक ऐसा relationship है, जिसमें दो objects सिर्फ आपस में interact करते हैं, लेकिन एक-दूसरे पर depend नहीं होते। दोनों independently exist कर सकते हैं।

Key Points 

  • Association एक general relationship है।
  • Objects एक-दूसरे से जुड़े होते हैं, लेकिन lifetime dependency नहीं होती।
  • दोनों objects अलग-अलग भी exist कर सकते हैं।
  • यह real world में communication या interaction को represent करता है।
  • Association को UML में simple solid line से represent किया जाता है।
  • ये Bidirectional या Unidirectional दोनों हो सकता है।

Syntax

class ClassA {
         // data members
};

class ClassB {
         ClassA obj; // Association
};

Example (उदाहरण)

Teacher और Student के बीच relation को Association माना जाता है, क्योंकि दोनों independently exist कर सकते हैं।

#include <iostream>
using namespace std;

class Student {
public:
         string name;
         Student(string n) {
               name = n;
}
};

class Teacher {
public:
          string name;

         Teacher(string n) {
              name = n;
}

// Association: Teacher uses Student
       void teach(Student &s) {
              cout << name << ” is teaching ” << s.name << endl;
}
};

int main() {
       Student s1(“Rahul”);
       Teacher t1(“Mr. Sharma”);

       t1.teach(s1); // Association: Teacher ↔ Student

       return 0;
}

Output: Mr. Sharma is teaching Rahul

Advantages of Association (लाभ)

  • Loose Coupling : दो objects एक-दूसरे से loosely जुड़े रहते हैं, जिससे पूरा system flexible और maintain करना आसान होता है।
  • Better Reusability : Classes independent होती हैं, इसलिए उन्हें multiple modules या applications में reuse किया जा सकता है।
  • Easy to Understand & Implement : Association एक simple relation है; beginners के लिए भी समझना आसान है।
  • Improved Modularity : हर class अपनी responsibility रखती है, जिससे code modular बनता है।
  • Low Impact on Changes : अगर एक class में change किया जाए तो दूसरी classes पर बहुत कम असर पड़ता है
  • Natural Real-World Mapping : Teacher–Student, Doctor–Patient जैसे real-world relations को represent करने के लिए best concept है |

Disadvantages of Association (हानियाँ)

  • Weak Relationship : Objects loosely connected होते हैं, जिससे ownership या strong dependency represent नहीं हो पाती।
  • Not Suitable for Complex Dependency Systems : जहाँ objects tightly connected हों (जैसे Part–Whole model), वहाँ Association fail हो जाता है; Composition की ज़रूरत पड़ती है।
  • Overuse : अगर Multiple classes unnecessarily associated हों, तो system complex और hard-to-manage बन जाता है।
  • No Lifetime Control : Association में यह control नहीं होता कि कौन-सा object कब create या destroy होगा, दोनों independently manage होते हैं।
  • Lacks Ownership Concept : Association यह नहीं बताता कि object का owner कौन है, जिससे relationship clarity कम रहती है |

Aggregation (एग्रीगेशन)

Aggregation OOP में एक special form of Association है, जहाँ एक object दूसरे object को contain करता है, लेकिन contained object independently exist कर सकता है।

Key Points

  • यह Association का special case है।
  • Container object के पास दूसरे object का reference होता है।
  • दोनों objects का lifetime अलगअलग होता है
  • Objects loosely connected होते हैं।
  • UML diagram में empty diamond (◊) से show किया जाता है।
  • Contained object अलग से create होता है और Multiple जगहों पर use हो सकता है।

Syntax 

class ClassA { };

class ClassB {
private:
          ClassA *obj; // Aggregation using pointer
public:
          ClassB(ClassA *o) {
              obj = o;
}
};

Example (उदाहरण)

College और Student – Student independently exist कर सकता है, लेकिन एक College अपने students की list contain करता है।

#include <iostream>
using namespace std;

class Student {
public:
        string name;
        Student(string n) {
          name = n;
}
};

class College {
private:
         Student *stu; // Aggregation (College HAS-A Student)
public:
        College(Student *s) {
          stu = s;
}

       void showStudent() {
           cout << “College has student: ” << stu->name << endl;
}
};

int main() {
       Student s1(“Amit”); // Student independently exist
       College c1(&s1); // Aggregation

       c1.showStudent();

       return 0;
}

Output: College has student: Amit

Advantages of Aggregation (लाभ)

  • Code Reusability : Contained objects अलग-अलग parts में reuse हो सकते हैं।
  • Loose Coupling: दोनों objects loosely connected होते हैं, flexibility बढ़ती है।
  • Independent Lifetime : Contained object को अलग से create और destroy किया जा सकता है।
  • Better Modular Design : हर class अपनी responsibility independently maintain करती है।
  • Clear Relationship Representation : “Has-A” relationship को simple और effective represent करता है।
  • Useful for Large Systems : जैसे ERP, Banking System, E-commerce apps — जहाँ कई independent components interact करते हैं।

 Disadvantages of Aggregation (हानियाँ)

  • Weak Ownership : Container class contained object की real ownership नहीं रखती।
  • No Lifetime Control : Container destroy होने पर भी contained object delete नहीं होता,  manually manage करना पड़ता है।
  • Misuse Increases Complexity : बहुत ज्यादा aggregation relations system को complex बना सकते हैं।
  • Not Suitable for Strong Dependency : जहाँ एक object दूसरे के बिना exist नहीं कर सकता, वहाँ aggregation useful नहीं होता हैं |
  • Object Management Overhead: Independently existing objects की memory और lifecycle programmer को ही manage करनी पड़ती है।

Composition (कॉम्पोज़िशन)

Composition Object-Oriented Programming में एक strong form of Association है, जिसमें एक class दूसरे class को पूर्ण रूप से own या contain करती है। इसमें contained object का life-cycle container object पर depend करता है। अगर parent object destroy हो जाता है, तो उसका contained child object भी destroy हो जाता है।

 Key Points

  • यह Strong (Has-a relationship) Association है।
  • Child object का life-cycle parent पर depend करता है।
  • Objects का tight coupling होता है।
  • More secure and controlled design, क्योंकि child object बाहर से access/modify नहीं होता।
  • Memory और structure पर complete control मिलता है।
  • UML में filled diamond (◆) से represent किया जाता है।

Syntax

class Part {
        // Child class
};

class Whole {
private:
       Part obj; // Composition: Whole HAS-A Part (strong relationship)

public:
      Whole() {
      // obj automatically created
}
      ~Whole() {
     // obj automatically destroyed WITH Whole
}
};

Example (उदाहरण)

class Engine {
public:
         void start() {
             cout << “Engine Started\n”;
}
};

class Car {
private:
        Engine eng; // Composition

public:
       void startCar() {
           eng.start();
           cout << “Car Running\n”;
}
};

int main() {
       Car c;
       c.startCar();
       return 0;
}

Advantages of Composition (लाभ)

  • Strong Ownership : Parent object contained objects का पूरा control रखता है।
  • Strong Lifecycle M anagement : Parent destroy होने पर child automatically destroy  हो जाता है |
  • Better Data Protection : Objects tightly controlled होने के कारण misuse या unpredictable behavior कम होता है |
  • High Cohesion : Class structure strong और meaningful बनती है।
  • Encapsulation Stronger : Object का internal structure अच्छी तरह secure रहता है।

Disadvantages of Composition (हानियाँ)

  • Tightly Coupled System : Objects एक-दूसरे पर बहुत depend होते हैं, flexibility कम हो जाती है।
  • Less Reusability : Contained object दूसरे किसी class में reuse नहीं किया जा सकता क्योंकि उसका lifetime parent पर depend करता है।
  • Complex Replacement : Parent class बदलने पर contained objects की structure redesign करनी पड़ती है।
  • Testing Harder : Unit testing में independent objects create करना कठिन।
  • Memory Overhead : Parent के destroy होते ही सभी child objects को भी destroy करना पड़ता है।

Difference Between Association, Aggregation, and Composition

Feature (विशेषता)Association (एसोसिएशन)Aggregation (एग्रीगेशन)Composition (कॉम्पोज़िशन)
Definition Objects आपस में interact करते हैं, लेकिन ownership नहीं होती।Whole-Part relationship, weak ownership, Part independent exist कर सकता है।Strong Whole-Part relationship, Part dependent होता है, Whole के बिना Part exist नहीं कर सकता।
Ownership कोई ownership नहींWeak ownership Strong ownership 
Part Lifespan Part अलग से exist कर सकता हैPart independent रहता है, Whole के बिना भी exist कर सकता हैPart का जीवन Whole से जुड़ा होता है, Whole खत्म हुआ तो Part भी खत्म
UML SymbolSimple lineHollow diamond near WholeFilled diamond near Whole
ExampleTeacher ↔ Student (Teacher और Student interact करते हैं)College → Students (Students बिना College के भी exist कर सकते हैं)House → Rooms (Rooms बिना House के नहीं exist कर सकते)
Strength WeakMediumStrong
Use Simple interaction, loosely connected objectsWeak Whole-Part relationship दिखाने के लिएStrong Whole-Part relationship दिखाने के लिए
error: Content is protected !!