Questions tagged [object-composition]

Object composition is about making more complex objects by assembling simpler objects. Do not use this tag for function composition .

Object composition is one of the main mechanism in object oriented programming to combine simpler objects into more complex ones.

For more details, see:

Do not use this tag for:

100 questions
0
votes
5 answers

A confusing object composition python code

Currently, I am on an online crash course on python and I encountered a confusing code. As shown below, it is a code that is designed to find out the number of cotton polo shirts. class Clothing: stock={ 'name': [],'material' :[], 'amount':[]} …
CD-ROM
  • 7
  • 1
  • 3
0
votes
1 answer

How to do train composition - compositon of classes

I am currently working on a train simulation project for uni. This is my class hierarchy: RollingStock Coach FreightCoach PassengerCoach SpecialCoach Engine DieselEngine ElectricEngine …
user12873296
0
votes
1 answer

Object composition in C++

I'm trying to implement a certain kind of object composition in C++. The idea is to create a set of all data structures which can be created by composing abstract classes that only know basic data structure operations. For example, a stack would be…
0
votes
1 answer

How can I create a list attribute that updates dynamically in Python?

I have two classes that refer to each other in a one-to-many relationship (Kid and Toy in the below example). When I assign a new Toy to Kid, I want the Kid to be assigned to Toy as well. Creating a custom class based on list for the toys attribute…
Jonathan Lym
  • 113
  • 8
0
votes
0 answers

Unable to apply composition over inheritance in Java Swing

I'm trying to adopt composition over inheritance when I create Swing components (which means I created a class that does not extend anything, specially not a Swing component such as JPanel) but I'm stumpling upon an issue with the Window Builder…
Piovezan
  • 3,215
  • 1
  • 28
  • 45
0
votes
2 answers

ES6 et al. is it possible to define a catch-all method?

Composition is a useful alternative to inheritance when one wants to cascade method calls from child to parent, see child extended class method calls its super version but that still only sees child data However, for the child to present an…
user10469346
0
votes
2 answers

Keeping self properties private with object composition

i was looking at a blog on object composition and it showed this code here: const canSayHi = self => ({ sayHi: () => console.log(`Hi! I'm ${self.name}`) }); const canEat = () => ({ eat: food => console.log(`Eating ${food}...`) }); const canPoop…
Dom
  • 1,232
  • 1
  • 10
  • 20
0
votes
1 answer

Passed an object instead of object's property?

There is two classes - Company and Project. Company object has property projects as list, that should indicate list of Project instances that is added to the company Here is realization of classes and methods to add projects to the company: class…
0
votes
2 answers

Passing variables using object composition in javascript

I've been trying to wrap my head around Object Composition for a while and I can't seem to find the "correct way" to do the same I was doing before with OOP. Lets say I have a class Entity with 3 variables, with OOP I would just create a class…
w1sh
  • 68
  • 9
0
votes
2 answers

How to use async code when previous implementation was synchronous

Recently I was learning about composition in object oriented programming. In many articles about it, there's an example of FileStore class (or however you call it) that implements interface like: interface IStore { void Save(int id, string…
Loreno
  • 668
  • 8
  • 26
0
votes
3 answers

Coding composition in java

Composition is a whole/parts relationship. A common way to code the composition relationship is to create the part object in the constructor of the composite (whole) object. Example in java: public class Car { private String brand; private…
Alan Allen
  • 71
  • 5
0
votes
1 answer

JS composition/inheritance prototype

I wish to be able to extend any object given in an .extend(obj) function. So far I have it working except for when an object literal is being passed in. example: class myClass { static extend(obj) { Object.assign(Object.getPrototypeOf(obj),…
Noel Heesen
  • 223
  • 1
  • 4
  • 14
0
votes
1 answer

Access member of composition class c++

I have the following code, there is an 'Face' class which is composed of an 'Eyebrow' class. The expression of the face can be changed to one of the allowed public enums, thus controlling how each of it's constituent should be changed. Each of the…
AndyM
  • 151
  • 1
  • 5
0
votes
1 answer

PHP object composition - access owner class' property without passing down arguments?

I have asked this question beforehand, but I might have worded it poorly. I did not get the answer. I have an Image class that always creates an instance of an Id class when it constructs. Each Image always has an Id, and when Image is destroyed,…
Dwarf Vader
  • 429
  • 1
  • 5
  • 14
0
votes
1 answer

Is this considered mutation from a Higher Order Component?

I was reading the section on Don’t Mutate the Original Component. Use Composition from this link. https://reactjs.org/docs/higher-order-components.html I then reviewed a project I'm trying to build. At a high level, this is what my code looks…
John
  • 32,403
  • 80
  • 251
  • 422