Questions tagged [composition]

Composition is the process of combining, or composing, parts of a program into a larger program.

1571 questions
1723
votes
32 answers

Prefer composition over inheritance?

Why prefer composition over inheritance? What trade-offs are there for each approach? When should you choose inheritance over composition?
readonly
  • 306,152
  • 101
  • 198
  • 201
436
votes
20 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
219
votes
11 answers

Orchestration vs. Choreography

What are the differences between service orchestration and service choreography from an intra-organization point of view.
PetrosB
  • 3,478
  • 4
  • 19
  • 21
219
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
  • 14,439
  • 26
  • 82
  • 107
199
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. Rails has yield statement. If React.js had yield statement, it…
NVI
  • 13,771
  • 16
  • 60
  • 102
104
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,018
  • 6
  • 28
  • 38
102
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
  • 27,150
  • 13
  • 69
  • 79
89
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,063
  • 5
  • 33
  • 50
82
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
  • 1,943
  • 13
  • 16
78
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,232
  • 21
  • 27
76
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,456
  • 30
  • 50
70
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
  • 38,095
  • 68
  • 199
  • 359
62
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
  • 9,810
  • 8
  • 39
  • 52
62
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
  • 3,734
  • 11
  • 44
  • 51
60
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,521
  • 1
  • 14
  • 20
1
2 3
99 100