bijan
bijan

Reputation: 1685

ARC declared property attributes - understanding

Is it correct, that the strong attribute refers to an object composition, and the weak attribute refers to object aggregation, in terms of UML notation?

Upvotes: 1

Views: 147

Answers (2)

Tricertops
Tricertops

Reputation: 8512

I don't think it has anything to do with difference between composition and aggregation, because both of them are used in both cases.

Examples from your link:

  • In case of one-to-one:
    1. Car has strong reference to Carburetor.
    2. Carburetor has weak reference to Car.
  • In case of one-to-many:
    1. Pond has strong reference to an array of Ducks.
    2. The array holds strong reference to each contained Duck.
    3. Duck has weak reference to pond.

In both cases there is owner with strong reference and owned objects with weak back-references.

So I would say that strong and weak are two endpoints of that UML connection line representing composition or aggregation.

Upvotes: 1

CRD
CRD

Reputation: 53000

Interesting question.

I would say they are similar but different.

In Objective-C the raison d'être for weak references is usually described as a way to manually break what would otherwise be a retain cycle - this is needed as ARC does not (at least currently) support collecting such cycles. This is not really a use of aggregation.

You will also see them used for things like notifications where one object will send notifications to another as long as the latter exists. This might be seen as closer to aggregation, but is really "do something if the target exists" and not "don't kill the target if I die".

You can of course use weak references in Obj-C to implement an aggregation, but you might not get exactly the behaviour you expect.

Consider the C++ example from your reference:

class Professor;

class Department
{
   ...
private:
   // Aggregation
   Professor* members[5];
   ...
};

Which in Objective-C might be:

@interface Department

@property (weak) NSArray *members;
...

@end

When the C++ object is destroyed the array is also destroyed but the elements of the array are not. However in Objective-C the array is not destroyed either - an NSArray always maintains strong references to its members, so they stay around. You could of course design weak collections for Objective-C.

Upvotes: 1

Related Questions