Bono
Bono

Reputation: 4849

Object acquaintance

I'm currently reading through the book Design Patterns by the gang of four. At one point Run-Time and Compile-Time structures are compared. In this section, the following is written:

Aggregation implies that one object owns or is responsible for another object. Generally we speak of an object having or being part of another object. Aggregation implies that an aggregate object and its owner have identical lifetimes.

My interpretation of that is something like the following in Java:

public class SomeClass{
    private OtherClass; //This is the aggregate object
    //...
}

I assume that this is what they mean, and I find that easy to understand. But then they define an acquaintance:

Acquaintance implies that an object merely knows of another object. Sometimes acquaintance is called "association" or the "using" relationship. Acquainted objects may request operations of each other, but they aren't responsible for each other. Acquaintance is a weaker relationship than aggregation and suggests much looser coupling between objects. ... In C++, aggregation can be implemented by defining member variables that are real instances, but it's more common to define them as pointers or references to instances. Aquaintance is implemented with pointers and references as well.

That final bit confuses me. Can it only be achieved through pointers? They're talking about C++ specifically, does this mean it cannot be accomplished in Java? To my understanding an aquaintance relation would be something like this in Java:

public class SomeClass{
    ///...

    public void doSomething(SomeOtherClass some_other_class){
        some_other_class.doSomethingElse(); //Is this (an) aquaintance?
    }

    ///...
}

I'm not entirely sure I've got this (aquaintance) concept down. Would anyone be willing to enlighten me if this is the correct way to view things in Java? Or can aquaintance relationships not be achieved in Java, because we don't have pointers?

Finally they say:

Ultimately, acquaintance and aggregation are determined more by intent than by explicit language mechanisms.

What is that supposed to mean? They are just abstract concepts, and not actual implementations or something?

Difference aggregation, acquaintance and composition (as used by Gang Of Four). This was the only question I could find that came close to my own, but it doesn't really help me much. Thanks in advance.

Upvotes: 5

Views: 1117

Answers (2)

Adrian Maire
Adrian Maire

Reputation: 14855

Aggregation implies that an aggregate object and its owner have identical lifetimes.

I disagree with this sentence, for the rest I will try to explain differences and give examples.


Acquaintance, Aggregation and Composition are modeling concepts: I mean they are not a programming technique nor something related with code, but a way to make a scheme from the world you are looking at.

Acquaintance example: You have a Personal computer and a Screen, the screen is not part of the PC nor the PC part of the screen, but both of them work together.

Aggregation example: A car has an engine, wheels and lights. The engine is part of the car, and the car is made with an engine. But if you remove the engine from the car, the car still exist.

Composition: Also call a "hard" aggregation: A football team is made of players and a player is part of a football team. If you remove all players of the team... well it isn't a team any more.

All of them: Acquaintance/Assotiation, Composition and Aggregation are concepts, they depend on how you look the world, and the separation is sometimes not very clear. In most case, it is more about what is important for you/your software.

There are "common" ways to implement all of them on Java and C++ (and you may easily find them), but again: the concept is the important here, not the code.

Upvotes: 2

dkatzel
dkatzel

Reputation: 31648

I think you understand everything perfectly. Acquaintance relations can be achieved in Java just like you say. (Java does have pointers; Java references are just a higher level of abstraction than C pointers)

public class SomeClass{
///...

    public void doSomething(SomeOtherClass some_other_class){
        some_other_class.doSomethingElse(); //yes SomeOtherClass is the Acquaintance
    }

///...
}

The GoF book was written before Java was released so it uses C++ terms and concepts that may or may not 100% match with Java.

The term acquaintance was probably to imply a less close relationship than the C++'s concept of a Friend which can see and access another object's private data

Upvotes: 5

Related Questions