Questions tagged [composition]

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

1737 questions
60
votes
4 answers

Aggregation vs Composition vs Association vs Direct Association

I am reviewing my knowledge in object-oriented programming. Under the relationship between classes topic, I have encountered some relationships which are a bit ambiguous to me. I know dependency "uses-a" and inheritance "is-a" but I'm a bit…
steven0529
  • 1,513
  • 1
  • 18
  • 28
60
votes
5 answers

C++ implicit copy constructor for a class that contains other objects

I know that the compiler sometimes provides a default copy constructor if you don't implement yourself. I am confused about what exactly this constructor does. If I have a class that contains other objects, none of which have a declared copy…
Jamison Dance
  • 19,896
  • 25
  • 97
  • 99
43
votes
6 answers

Difference between trait inheritance and self type annotation

In Scala, I've seen the constructs trait T extends S and trait T { this: S => used to achieve similar things (namely that the abstract methods in S must be defined before an instance may be created). What's the difference between them? Why…
Ben Lings
  • 28,823
  • 13
  • 72
  • 81
42
votes
16 answers

Composing functions in python

I have an array of functions and I'm trying to produce one function which consists of the composition of the elements in my array. My approach is: def compose(list): if len(list) == 1: return lambda x:list[0](x) list.reverse() …
40
votes
3 answers

Haskell Monad bind operator confusion

Okay, so I am not a Haskell programmer, but I am absolutely intrigued by a lot of the ideas behind Haskell and am looking into learning it. But I'm stuck at square one: I can't seem to wrap my head around Monads, which seem to be fairly fundamental.…
Ord
  • 5,693
  • 5
  • 28
  • 42
40
votes
3 answers

Canvas image masking / overlapping

In my project i have to implement one different color image on the other same size and pattern image using canvas and images are not in round or rectangle shapes. That all are in waves shape and it will apply on a single main background image for…
Anup
  • 3,283
  • 1
  • 28
  • 37
39
votes
1 answer

Design patterns: Composite vs. Composition

I am finishing a course on design patterns, and while reviewing the notes came across something I missed during the semester: Composite vs. Composition. What I managed to understand is that composite is when an object actually encapsulates whole…
Baruch
  • 20,590
  • 28
  • 126
  • 201
38
votes
12 answers

Object Oriented Best Practices - Inheritance v Composition v Interfaces

I want to ask a question about how you would approach a simple object-oriented design problem. I have a few ideas of my own about what the best way of tackling this scenario, but I would be interested in hearing some opinions from the Stack Overflow…
Mark Heath
  • 48,273
  • 29
  • 137
  • 194
33
votes
8 answers

Does C# support function composition?

In the latest version of C#, can I do something like Haskell's function composition? more...? Function composition is the act of pipelining the result of one function, to the input of another, creating an entirely new function. I feel like linq is…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
32
votes
6 answers

inheritance vs. composition for testability

While designing my objects I find composition to be a better choice from the perspective of testability. The reason being, I can mock parts of the composition structure if I need to, while running unit tests. This is not possible if I have an…
Parag
  • 12,093
  • 16
  • 57
  • 75
32
votes
6 answers

Extends JFrame vs. creating it inside the program

When making an application using Swing, I've seen people do one of the two things to create a JFrame. Which is a better approach and why? I'm a beginner at Java and programming. My only source of learning is books, YouTube and Stack Overflow. import…
satnam
  • 1,457
  • 4
  • 23
  • 43
32
votes
5 answers

C# - Object Composition - Removing Boilerplate Code

Context / Question I've worked on numerous .NET projects that have been required to persist data and have usually ended up using a Repository pattern. Does anyone know of a good strategy for removing as much boilerplate code without sacrificing…
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
31
votes
4 answers

How to compose `not` with a function of arbitrary arity?

When I have some function of type like f :: (Ord a) => a -> a -> Bool f a b = a > b I should like make function which wrap this function with not. e.g. make function like this g :: (Ord a) => a -> a -> Bool g a b = not $ f a b I can make…
31
votes
4 answers

Difference between dependency and composition?

Definitions taken from here Dependency Change in structure or behaviour of a class affects the other related class, then there is a dependency between those two classes. It need not be the same vice-versa. When one class contains the other…
danihodovic
  • 1,151
  • 3
  • 18
  • 28
30
votes
5 answers

Is there anything composition cannot accomplish that inheritance can?

Composition and inheritance. I am aware that they are both tools to be chosen when appropriate, and context is very important in choosing between composition and inheritance. However, the discussion about the appropriate context for each is usually…
David V McKay
  • 122
  • 2
  • 12