Questions tagged [composition]

Composition means to assemble simpler elements into more complex structures. In OOP, composition usually refers to object composition, i.e. assembling several simpler objects into a a more complex aggregate. It may also refer to an aggregation of object, where the aggregate owns some components.

Filter by
Sorted by
Tagged with
0 votes
0 answers
31 views

Structure and usage of complex React components

Let's say I have following footer on my website. Now, I am trying to figure out, how to implement it as React component properly. Note that subcomponents have dynamic content (list of links, partners....
0 votes
0 answers
45 views

Is this the main difference between Composition and Aggregation? [duplicate]

I think the difference between Composition and Aggregation is as follows. Composition: When the parent object is killed, the child object cannot be used by another object. Ex: Employee and BankAccount....
0 votes
2 answers
264 views

Inheritance/Composition VS "Direct Injection Construction"

my following example seems to go into the direction Inheritance VS Composition. But that's not, what i want to ask. I see the concept Inheritance and Composition on one side and the alternative, which ...
0 votes
2 answers
119 views

When using composition when to favour wrapper methods?

When using composition, when should we favour wrapper methods? Let's say we have the classic car example. public class Car { private Battery battery; //wrapper method public void load(...
  • 29
0 votes
1 answer
113 views

If we don't use function composition, does Maybe remain a monad?

A monad is a monoid in the category of endofuctors. Category is a set of two things: Set of elements Set of binary operations between these elements. When we talk about the category endofunctors we ...
  • 449
49 votes
7 answers
10k views

Why is inheritance bad in a Person-Student model?

I've just started learning about Inheritance vs Composition and it's kind of tricky for me to get my head around it for some reason. I have these classes: Person class Person { public string Name {...
3 votes
4 answers
598 views

(How) can the circle-ellipse problem be solved by using composition rather than inheritance?

I was reading about composition over inheritance and came across a question about solving the Circle-Ellipse Problem in Object-Oriented Programming. This kind of problem is often used as an example of ...
  • 147
5 votes
3 answers
146 views

UML Composition parent association end

The Wikipedia article on the Composite Design Pattern includes the following diagram: As you see, there is an association relationship which is child 0..* – 1 parent (association). However, shouldn't ...
1 vote
1 answer
223 views

UML Composition parent multiplicity

The Wikipedia article on the Composite Design Pattern includes the following diagram: As you see, there is an association relationship which is child 0..* – 1 parent. However, given that: a ...
0 votes
2 answers
127 views

How to solve an issue when a decorator needs variables from the base class?

I have a service class that does some magic. I want to introduce a new type of functionality - raise an event. I am absolutely sure that decorator pattern is great for this scenario. The problem is ...
  • 449
5 votes
2 answers
1k views

Design classes to model 3D scanned faces of ancient Greek/Roman sculptures: is multiple inheritance a good design solution?

I would like to deepen the topic of multiple inheritance using Python and I usually find examples that are too simple. I love art and I imagined the following problem and I want to understand if ...
  • 388
1 vote
1 answer
155 views

State Pattern when the Behavior in Object of Type A is Dependent on the State of the Object of Type B

I have used the state pattern for my web application where a user can be in one of three states: activated, required_activation and suspended. I have created 3 states classes as given below (source is ...
  • 137
1 vote
4 answers
104 views

Class Diagram - Aggregating over a class that has physical presence or logical presence?

In the below figure, a CD shop has CD cupboards, and inside it has shelves. CDs could be rented, and class CD Copy represents the actual CDs that are rented. My question is, should I represent Shelves ...
  • 339
0 votes
2 answers
291 views

Design for three classes with partially shared logic

I am looking for a suitable design which uses composition to allow three classes to share some logic. The problem I am solving is that I have to read 3 different json configuration files from my ...
  • 127
9 votes
6 answers
2k views

Composition over inheritance when adding functionality to a foreign object

Note: I am not sure of the correct terminology for what I am describing, so if you have suggestions for a better title please feel free to edit it. "Favor composition over inheritance" is ...
2 votes
4 answers
305 views

How to make components communicate with each other

I have a simple setup where I've decided to reduce my overall OOP design and opt for lots of small individual components that I can add and remove from a particular object. The whole point of this was ...
  • 2,032
2 votes
1 answer
137 views

How to implement Gang of Four's Lexi's Compositor-Composition?

I'm studying Design Patterns book and I was trying to understand the Composition principle in Lexi's Compositor-Composition as explained in figure 2.5, as well as how to implement it. Where the ...
2 votes
0 answers
96 views

Is there an elegant way to handle run-time dependent object composition without using dynamic_cast?

Context I'm attempting to design a modelling framework that is modular and easy to extend and test. (I'm trying to follow open/closed, dependency injection, and composition over inheritence principles)...
  • 29
1 vote
4 answers
751 views

How can composed sub-objects access the parent object?

Let's say I want to make a class for a car. A car has an engine, transmission, steering, radio, ... Each of these parts have its own internal state, so in C++ it makes sense to use composition like ...
  • 157
10 votes
4 answers
2k views

Can a class contain its own class?

Suppose I have the following class structure: A forest can have any number of trees, but each tree can belong to only one forest. If the forest is deleted, the tree is deleted. A tree must have at ...
  • 219
2 votes
1 answer
125 views

Loose coupling vs transparency in visitor pattern that visits composite

I have a composite object (I will call it A) that has fixed number of sons (I will call them A1 and A2) All of them have an Accept(Visitor) method. I have a GUI, A is the main window that consists of ...
  • 117
-4 votes
3 answers
211 views

Confused between HAS-A and representation [closed]

I'm currently working on a game in which a maths module is being used, here is one of the classes called Vertex. I'm getting confused with the relationship between HAS-A and objects representation. I ...
0 votes
1 answer
78 views

Composition without direct instantiation through constructor

I have 2 classes that independently operate on specific file formats: class FileWriterA and class FileWriterB. Each class has some shared methods (e.g., __enter__, __exit__, __str__, closed, is_closed,...
  • 1,017
4 votes
3 answers
480 views

Inheritance vs composition: How would you implement an "unordered list"? Subclass of list, or composition?

This came up at work and left me thinking about the best way to model this: In Python, we have the built-in list container, which is a mutable sequence. Equality between two lists is defined as ...
  • 516
1 vote
3 answers
188 views

Is it called "composition" if we are using primitive data types?

The following is an example of "composition": public class Car { Engine engine; // Engine is a class } But is it still called "composition" if we are using primitive data ...
3 votes
2 answers
141 views

Composing and Inheriting from the Same Type

To start off with an example: I have a read-only Repository used for getting arbitrary values. This behavior can be implemented multiple ways. I also want to allow opt-in mutation of the repository's ...
2 votes
1 answer
666 views

How to design a social media follower/followed relation with UML?

I am working on a UML diagram for social media website like Facebook: The main class of this system is User, which has members as userid, email, phone, address, Friends, etc. A user may follow a set ...
1 vote
2 answers
142 views

Choosing between DI (without container) and Composition

I'm working on a classes that are designed to read a defined file format; said format is comprised of 2 separate files (FileA and FileB). For ease of use, the format is referred to as the FileA format,...
  • 1,017
-4 votes
1 answer
136 views

Alternatives to inheritance for polymorphic behavior for data in database?

I am thinking of designing a website where people can enter their aliases for games they played in the past and they can search for others around the same time period. FWIW I don't care if it exists ...
0 votes
1 answer
479 views

Is it an anti-pattern on React to search for children with a specific element type?

Context When designing the API of a React component, you may decide to receive props in a more semantical way. Here's a simple example <Modal headerTitle="foo" /> vs <Modal> &...
-2 votes
1 answer
40 views

Calculating position of menu options with variable sizes

Not entirely sure if this is the most appropriate community for this but... I'm looking for solutions to calculate positions for menu options when the amount of options vary and when each options ...
0 votes
1 answer
243 views

Extension of classes - Where to put behaviour - How much direct-access is allowed

At this point i would exclude the inheritance.... So the question is about more like extend into a seperate class or into the class which should be extended, both play in the composition-league. So ...
1 vote
2 answers
96 views

What does this statement about implementing forwarding methods of components mean?

I can't wrap my head around this statement here: One common drawback of using composition instead of inheritance is that methods being provided by individual components may have to be ...
  • 119
0 votes
1 answer
318 views

favor composition over inheritance: practical problems

In a C# application, I've got a behaviour that I would like to be available in different classes that not necessarily share the same ancestor. What better opportunity to 'favour composition over ...
  • 841
1 vote
1 answer
66 views

Is this form generator a valid Composite GoF?

Searching for a good PHP real world example, I've found this example of "composite" using: FormElement as Component Fieldset and Form as Containers Input as Leaf (this is my UML from the code): ...
  • 243
1 vote
2 answers
80 views

Composite Design Pattern with unequal weightage

You have a video game in which upon killing final boss you get coins that get distributed based on whether you are: Person(individual) Group(consists of individuals or groups) If reward for killing ...
0 votes
2 answers
274 views

Example of NOT associative (not composable) function

Bartosz Milewski, in his book "Category Theory for Programmers" says that: Composition is associative. If you have three morphisms, f, g, and h, that can be composed (that is, their objects match ...
  • 119
1 vote
3 answers
356 views

what it means when someone says - "statements/instructions are not composable"?

I have been using c# and trying to learn FP. In context of FP I often hear that usage of basic assignment or return statements are not considered composable, hence their usage is not advised in FP ...
18 votes
11 answers
10k views

Need Good OOP Design For World and Countries Problem

I am currently working on problem with a chicken or egg first situation. Basically, I am designing a solution which goes like this: World is a collection of countries; Each Country has a name, flag ...
  • 345
9 votes
2 answers
743 views

Why are impure functions said to be non-composable?

I understand what pure functions are and when someone says pure functions are composable - I believe it means that the output of one function can be passed as an input to another function but same ...
4 votes
1 answer
1k views

Delegate vs Forwarding in Java OOP

I'm reading some article about "prefer composition over inheritance", and heard about Forwarding and Delegation. After search for the different I found some source: https://en.wikipedia.org/wiki/...
4 votes
3 answers
5k views

Interfaces in Python, multiple inheritance vs. a home-made solution

I am writing a Python framework. In order to ensure a class has some properties, I make base "interface" classes like: class BananaContainer: def __init__(self): self._bananas = [] @...
  • 182
0 votes
2 answers
133 views

Modelling 2 composition relationships between 2 XML elements using UML

I want to model an XML schema that has the following composition relationships: A can contain B, and in another case B can contain A. Using a bi-directional composition is not an option here as it is ...
1 vote
1 answer
127 views

Structuring a pipe for function composition when an intermediate result is needed by later functions

I have a set of pure functions that can be composed almost trivially in a pipe as initialValue -> [f] -> [g] -> [h] -> ... -> [m] -> [n] -> [o] -> outputValue The problem I ...
  • 109
1 vote
1 answer
1k views

Avoid too much dependencies for a Use Case in a Clean Architecture app

Context I'm currently developing an application following the Clean Architecture principles (at least I'm trying really hard to follow these). All my Dependency Injections are done manually, without ...
1 vote
1 answer
513 views

How to define Composition Roots for libraries (Javascript/inversify)

I'm building a console app (A), which depends on library B. Library B further depends on library C. I am building all 3 entities. Most of the development completed so far has been done using a ...
1 vote
3 answers
273 views

Applying Composition Over Inheritance to Page objects

I'm trying to figure out how to apply composition over inheritance to a system I'm building, and tripping up when I try to apply it to my page objects. The relevant objects are: PageFactory Page ...
9 votes
9 answers
1k views

Inheritance vs Composition For Chess Pieces

A quick search of this stackexchange shows that in general composition is generally considered more flexible than inheritance but as always it depends on the project etc and there are times when ...
0 votes
1 answer
963 views

DDD & composition

I have a project to build, which has the following requirements: topup a wallet by paying money in exchange for credit; this has also the constraint of needing an approval from the finance team, if ...
  • 48
2 votes
4 answers
721 views

Unit Testing order with contained & inherited objects

I'm confused about unit testing order. Many posts in StackOverflow say that unit test should be independent and small. IMHO, in the case of aggregation or composition, the member objects should be ...