Composition is the process of combining, or composing, parts of a program into a larger program.
Questions tagged [composition]
1737 questions
1962
votes
35 answers
Prefer composition over inheritance?
Why prefer composition instead of inheritance? What trade-offs are there for each approach? And the converse question: when should I choose inheritance instead of composition?

readonly
- 343,444
- 107
- 203
- 205
563
votes
21 answers
What is the difference between association, aggregation and composition?
What is the difference between association, aggregation, and composition?
Please explain in terms of implementation.
None
281
votes
11 answers
Orchestration vs. Choreography
What are the differences between service orchestration and service choreography from an intra-organization point of view.

PetrosB
- 4,134
- 5
- 22
- 21
249
votes
17 answers
Difference between Inheritance and Composition
Are Composition and Inheritance the same?
If I want to implement the composition pattern, how can I do that in Java?

gmhk
- 15,598
- 27
- 89
- 112
223
votes
3 answers
React.js: Wrapping one component into another
Many template languages have "slots" or "yield" statements, that allow to do some sort of inversion of control to wrap one template inside of another.
Angular has "transclude" option.
Ruby/Rails has yield statement. If React.js had yield statement,…

NVI
- 14,907
- 16
- 65
- 104
115
votes
9 answers
Implementation difference between Aggregation and Composition in Java
I'm aware of the conceptual differences between Aggregation and Composition. Can someone tell me the implementation difference in Java between them with examples?

Rajath
- 2,178
- 6
- 32
- 38
109
votes
10 answers
Haskell composition (.) vs F#'s pipe forward operator (|>)
In F#, use of the the pipe-forward operator, |>, is pretty common. However, in Haskell I've only ever seen function composition, (.), being used. I understand that they are related, but is there a language reason that pipe-forward isn't used in…

Ben Lings
- 28,823
- 13
- 72
- 81
92
votes
7 answers
Java - Method name collision in interface implementation
If I have two interfaces , both quite different in their purposes , but with same method signature , how do I make a class implement both without being forced to write a single method that serves for the both the interfaces and writing some…

Bhaskar
- 7,443
- 5
- 39
- 51
87
votes
5 answers
Concrete example showing that monads are not closed under composition (with proof)?
It is well-known that applicative functors are closed under composition but monads are not. However, I have been having trouble finding a concrete counterexample showing that monads do not always compose.
This answer gives [String -> a] as an…

Brent Yorgey
- 2,043
- 16
- 17
81
votes
2 answers
Mixins vs composition in scala
In java world (more precisely if you have no multiple inheritance/mixins) the rule of thumb is quite simple: "Favor object composition over class inheritance".
I'd like to know if/how it is changed if you also consider mixins, especially in…

Sandor Murakozi
- 4,372
- 25
- 27
77
votes
1 answer
Application architecture/composition in F#
I have been doing SOLID in C# to a pretty extreme level in recent times and at some point realized I'm essentially not doing much else than composing functions nowadays. And after I recently started looking at F# again, I figured that it would…

TeaDrivenDev
- 6,591
- 33
- 50
76
votes
6 answers
What is composition as it relates to object oriented design?
I hear (and read on this site) a lot about "favour composition over inheritance".
But what is Compositon? I understand inheritance from the point of Person : Mammal : Animal, but I can't really see the definition of Compostion anywhere.. Can…

DaveDev
- 41,155
- 72
- 223
- 385
67
votes
13 answers
Why use inheritance at all?
I know the question has been discussed before, but it seems always under the assumption that inheritance is at least sometimes preferable to composition. I'd like to challenge that assumption in hopes of gaining some understanding.
My question is…

KaptajnKold
- 10,638
- 10
- 41
- 56
64
votes
5 answers
Distinguishing between delegation, composition and aggregation (Java OO Design)
I am facing a continuing problem distinguishing delegation, composition and aggregation from each other, and identifying the cases where it's the best to use one over the other.
I have consulted a Java OO Analysis and Design book, but my confusion…

denchr
- 4,142
- 13
- 48
- 51
61
votes
3 answers
Composition, Inheritance, and Aggregation in JavaScript
There is a lot of information about composition vs inheritance online, but I haven't found decent examples with JavaScript. Using the below code to demonstrate inheritance:
function Stock( /* object with stock names and prices */ ) {
for (var…

Brian
- 1,571
- 1
- 15
- 20