如何根據編程代碼顯示聚合? (How to Show Aggregation in terms of A Programming Code?)


問題描述

如何根據編程代碼顯示聚合? (How to Show Aggregation in terms of A Programming Code?)

I know about Association and Aggregation and Composition and Generalization what they are by definition. inheritance is "is a" relationship and composition is "has a" relationship. 

Class A {
}

Class B extends A { // this is Generalization
}

Class C {
 A ob;  // this is composition
}

now my question is how the Aggregation and simple Association is shown in terms of Programming code. ?


參考解法

方法 1:

I suspect your real question has to do with composition versus aggregation.  You can think of the difference in terms of ownership but the real distinction (for my money) is what controls the lifecycle of aggregated object.

In composition. when the composed object is destroyed its contained parts or classes are destroyed along with it.  With aggregation, the contained object's lifetime can be independent of the containing object.  In code. this comes down to whether the component object is specified by value or reference.  Aggregation has to be done by reference (or pointer as in the example).  If it is done by value the component part will go out of scope and be destroyed with containing object and is thus composition.

So in this case Engine is an example of composition and Battery an example of Aggregation.

#include <iostream>

using namespace std;

class Engine
{
   public:

      Engine() {cout << "Engine created\n";};
     ~Engine() {cout << "Engine destroyed\n";};
};


class Battery
{
   public:

      Battery() {cout << "Battery created\n\n";};
     ~Battery() {cout << "\nBattery destroyed\n";};
};

class Car
{
   private:

      Battery *bat;
      Engine  eng;  //Engine will go out of scope with Car

   public:

      Car(Battery* b) : bat(b) {cout << "Car created\n";};
     ~Car() {cout << "Car destroyed\n";};

       void drive(int miles) {/*...*/};
};



int main(int argc, char *argv[])
{
   //a Battery lifecycle exists independently of a car
   Battery* battery = new Battery();

   //but a car needs to aggregate a Battery to run
   Car* car1 = new Car(battery);

   car1->drive(5);

   //car1 and its Engine destroyed but not the Battery
   delete car1;

   cout << "---------------\n";

   //new car, new composed Engine, same old Battery
   Car* car2 = new Car(battery);

   car2->drive(5);
   delete car2;

   //destroy battery independently of the cars
   delete battery;

}

Apologies if this is not the best example but hopefully it illustrates the main point.

方法 2:

I'm not sure exactly what you're going for here, but I would suggest the following examples:

Aggregation

public class A { }
public class List<A> { }  // aggregation of A

Association (uses)

public class A
{
    public void AMethod() { ... }

public class B
{
    public void BMethod( A a )
    {
         a.AMethod();  // B uses A
    }
}

(by g.revolutionDucktvanfosson)

參考文件

  1. How to Show Aggregation in terms of A Programming Code? (CC BY-SA 3.0/4.0)

#ooad #aggregation #associations






相關問題

接口實現是否應該獨立 (Should Interface implementations be independent)

當我們有不同的回報類型時實現策略模式 (Achieve strategy pattern when we have different return type)

抽象VS信息隱藏VS封裝 (Abstraction VS Information Hiding VS Encapsulation)

應用程序中jQuery表單綁定代碼的最佳實踐 (Best practices with jQuery form binding code in an application)

耦合與內聚 (Coupling and cohesion)

什麼時候應該在C ++中使用類與結構? (When should you use a class vs a struct in C++?)

What is an anti-pattern? (What is an anti-pattern?)

如何根據編程代碼顯示聚合? (How to Show Aggregation in terms of A Programming Code?)

協調 MVP 三元組 (Coordinating MVP triads)

裝飾器和虛擬方法 (Decorators and Virtual Methods)

設計和建築有什麼區別? (What is the difference between Design and Architecture?)

特定時間段有效的業務規則——如何有序管理 (Business rules that are valid for specific time span – how to manage in an orderly manner)







留言討論