Composition is the process of combining, or composing, parts of a program into a larger program.
Questions tagged [composition]
1737 questions
18
votes
1 answer
Expressive and composable error types
I am struggling with the best way to report errors in a set of functions that should compose nicely, in a library I'm working on.
Concretely, I have functions that look like:
foo, bar, baz :: a -> Maybe a
where foo can fail in only one way (a good…

jberryman
- 16,334
- 5
- 42
- 83
18
votes
2 answers
A Request for Simple C++ Composition vs. Inheritance Examples
I am trying to understand the syntactic difference between composition and inheritance in C++.
I'm hoping someone will provide two simple examples. One example of a class that uses composition and one of a class that uses inheritance.

LuminousNutria
- 1,883
- 2
- 18
- 44
18
votes
2 answers
When to use association, aggregation, composition and inheritance?
I've seen plenty of posts on Stackoverflow explaining the difference between the relationships: associations, aggregation, composition and inheritance, with examples. However, I'm more specifically confused more about the pros and cons of each of…

daydreamingwill
- 313
- 1
- 2
- 9
18
votes
2 answers
How to redirect all methods of a contained class in Python?
How to implement the composition pattern? I have a class Container which has an attribute object Contained. I would like to redirect/allow access to all methods of Contained class from Container by simply calling…

Yariv
- 12,945
- 19
- 54
- 75
17
votes
11 answers
C# OOP Composition and Generalization at the same time
This might be a simple/basic OOP question, but I still cannot figure out how to solve it.
I had the following problem during an interview : make an UML class diagram and write the basic code for a "smart"phone which contains the functionalities of…

IonR
- 173
- 1
- 5
17
votes
4 answers
When is a composition of catamorphisms a catamorphism?
From page 3 of http://research.microsoft.com/en-us/um/people/emeijer/Papers/meijer94more.pdf:
it is not true in general that catamorphisms are closed under composition
Under what conditions do catamorphisms compose to a catamorphism? More…

Sebastien
- 341
- 1
- 7
16
votes
2 answers
TypeScript: class composition
Based on this awesome Composition over Inheritance video by MPJ, I've been trying to formulate composition in TypeScript. I want to compose classes, not objects or factory functions. Here is my effort so far (with a little help from lodash):
class…

Glenn Mohammad
- 3,871
- 4
- 36
- 45
16
votes
1 answer
Should DTOs use inheritance or composition
In SOA if a few DTO classes have some fields that are repeated across. Is it better to use Composition or Inheritance so there is not repetition OR just use one DTO class that encapsulates all fields.As my DTO classes grow I see lot of repetitive…

pingu
- 645
- 3
- 11
- 21
15
votes
7 answers
What does it mean for something to "compose well"?
Many a times, I've come across statements of the form
X does/doesn't compose well.
I can remember few instances that I've read recently :
Macros don't compose well (context: clojure)
Locks don't compose well (context: clojure)
Imperative…

letronje
- 9,002
- 9
- 45
- 53
15
votes
4 answers
python: inheriting or composition
Let's say that I have class, that uses some functionality of dict. I used to composite a dict object inside and provide some access from the outside, but recently thought about simply inheriting dict and adding some attributes and methods that I…

gruszczy
- 40,948
- 31
- 128
- 181
15
votes
1 answer
Should I use inheritance or composition?
I would like to keep this one short. I build a HouseA that has two rooms, say BedRoom and StudyRoom, both deriving from a base class called Room.
BedRoom and StudyRoom have a same parent called House. Also, any room in a house can access any other…

rajachan
- 795
- 1
- 6
- 20
14
votes
2 answers
why inheritance is strongly coupled where as composition is loosely coupled in Java?
I have heard this favor composition over inheritance again and again in design patterns. some of the reasons cited for this are
1)Inheritance is strongly coupled where as composition is loosely coupled
2) Inheritance is compile time determined where…

brain storm
- 30,124
- 69
- 225
- 393
14
votes
1 answer
How to customize h:head when using ui:composition template?
I am using JSF to render an HTML page. I design the page like it :

le2tbomi
- 165
- 1
- 1
- 8
13
votes
1 answer
Why are monads not closed under composition?
While I was learning Composing Types chapter from Haskell Book, I was given tasks to write Functor and Applicative instances for the following type.
newtype Compose f g a = Compose { getCompose :: f (g a) }
I wrote the following…
user10891762
13
votes
3 answers
How do I derive the type for this function:
I'm trying to get better at playing "type tetris". I have the functions:
(=<<) :: Monad m => (a -> m b) -> m a -> m b
zip :: [a] -> [b] -> [(a, b)]
And GHCi tells me:
(zip =<<) :: ([b] -> [a]) -> [b] -> [(a, b)]
I'm having a hard time figuring out…

Cameron Ball
- 4,048
- 6
- 25
- 34