lunar
lunar

Reputation: 1232

Aggregation or composition

I am bit confused so if you can enlighten me please.

  1. When I create an object through new operator in my class or using Singleton is it a composition?
  2. When I call a factory method is it composition too?

Upvotes: 2

Views: 506

Answers (2)

Dave
Dave

Reputation: 4597

Composition vs. Aggregation has to do with component object lifetime, not how the component objects are instantiated. If component objects are disposed of when the containing object is disposed of, it's Composition. If the component objects can live on, it's Aggregation.

Upvotes: 3

eulerfx
eulerfx

Reputation: 37719

The term "composition" has slightly different meaning within UML and OOP.

In UML, composition is a stronger form of aggregation and is sort of like a "owns-a" association.

The GoF design pattern book suggests that in OOP, one should favor composition over inheritance. In this case, composition just means that functionality is provided by referencing a class rather than inheriting from it.

It seems that you have some terms mixed up. Creating and object or calling a factory method is not in itself composition.

Upvotes: 2

Related Questions