Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
How is instantiation different from aggregation and from acquaintance? (I have undertood the difference between aggregation and acquaintance, see the quote from the book here)
Instantiation and aggregation seem similar to each other.
when object A instantiates or aggregate object B, it seems to me that object A must have a field member referencing object B.
Both instantiation and aggregation seem to me not to allow more than one objects to instantiate or aggregate the same object.
There might also be other aspects which I miss to consider
I have asked What does "a class instantiates another" mean?, but I am still not very clear about instantiation relation between classes.
Instantiation of an object is the process of creating instance of an object and is executed by the compiler. You can instantiate as often as you want or as memory is available, since instantiation means allocation of memory. The instance (as a result of the instantiation) is the actual allocated memory area to store all data of the object (e.g. fields, variables). The class definition describes the layout of this allocated memory area. On the other hand aggregation, as well as acquaintance, describes the relation of two or more objects and how their life cycles are linked to each other. Relation in case of acquaintance or aggregation means ownership. Aggregation means when the owner instance is destructed (removed from memory) then the owned (aggregated) instance will be destructed as well. Acquaintance means when the owner instance is destructed, then the owned instance will continue to exist in memory. Instantiation creates an instance by allocating the memory that is later released when the instance gets destructed.
Let's look at a house. Maybe we find some analogy here that can help you to understand it better:
the class or object is like a construction plan, that was created by an architect (architect = developer). This plan tells the builder (compiler) how to build the house. The finished house is the manifestation of the construction plan, and the instance is the manifestation of an object (the class). So instance only means an object is created, existent and alive.
All the entities of a house like stairs and rooms are also objects on their own that were build (instantiated) in size and location according to the architect's plan. This means a house is an object instance that consists of other object instances. All those instances coexist to create the building, but when having a closer look we can describe this coexistence as a relationship: The house has rooms or it owns rooms. We can even look more closer at this relationship and ask our self what the effects of this relationship are and if also other kind of relationships do exist, when we are looking at the house. Martin Fowler, an architect himself, found and described two different kinds:
Aggregation is when the room instances of a building instance are destroyed when the owner (the building instance) is destroyed. A room instance actually exist only in one building instance (owner or object instance).
Acquaintance is when the tenant is still alive when the building is destroyed. He would move to another building to rent a flat. The tenant can rent multiple places in different houses. A tenant is another entity or object instance that is related to the house, whereas the tenant's DNA is the object or class, it's the creator's construction plan that tells him how the instance has to look like and what attributes he will have once instantiated (born).