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
3
votes
1 answer

Why use this.props.children?

There are other similar questions that have been "answered", but the answers aren't complete and they don't solve the real problem. This is really a two-parter with a sub-question. 1. What are the general situations you would want to use…
Urasquirrel
  • 1,461
  • 1
  • 18
  • 34
3
votes
1 answer

Composition instead of inheritance

I'm developing a WPF with MVVM pattern, .NET Framework 4.6.1. and C#. My question is not about WPF, it's about using composition instead of inheritance with these two classes: public class ObservableObject : INotifyPropertyChanged { public event…
VansFannel
  • 45,055
  • 107
  • 359
  • 626
3
votes
4 answers

Is multiple inheritance needed?

I have a situation like below: class A { virtual void f() { /*some default impl here*/} virtual void g() { /*some default impl here*/} }; class B : public A { virtual void f() { /* do some specific stuff and call parent's f()*/} …
Heghine
  • 435
  • 3
  • 15
3
votes
2 answers

Can't get composition right

I'm honestly embarassed I have to ask but I'm stuck on that. #include using namespace std; class Obj { }; class Test { private: Obj a; public: Test(Obj _a) : a(_a) {} }; int main() { Obj ob(); Test t(ob); return…
3
votes
7 answers

Is there Method Inheritance in C#

I am wondering whether there is any feature like method inheritance rather than whole class inheritance, let me elaborate what I am trying to explain : class a { public void GetA(){ // some logic here } } class b { public void GetB() :…
Tarik
  • 79,711
  • 83
  • 236
  • 349
3
votes
5 answers

Replacing member objects with subclasses in Python

I have the following problem that I will attempt to illustrate with the following example. class Brick(): def __init__(self): self.weight = 1 class House(): def __init__(self, number_bricks): self.bricks = [Brick() for i in…
2
votes
1 answer

What is the correct way to access a class composite inside one of its components, if that's even advisable?

Suppose I have these two classes: class Branch(): def __init__(self, id, leaves): self.id = id self.leaves = leaves class Leaf(): def __init__(self, color) self.color = color def describe() …
Bernardozomer
  • 57
  • 2
  • 7
2
votes
2 answers

Methods on an object created via composition can't access all properties

I'm building the concept of a family for a product, with members being of different types (accountHolder, payingCustomer, student, and so on). Originally I built these as sub-classes of FamilyMember, but I ended up with some repeated code and…
filipe
  • 414
  • 1
  • 5
  • 14
2
votes
3 answers

Mixing Composition and Inheritance in Java

Is it possible to mix composition and inheritance in Java? Fist I have some generic classes is a HAS-A (or HAS-MANY) relationship (Composition). Generic classes: Structure, TypeA, TypeB, TypeC, etc., where Structure is in a HAS-A relationship with…
mimi
  • 31
  • 6
2
votes
0 answers

Given a recompose lifecycle HOC, how to avoid needing to describe the PropTypes?

I've created a recompose lifecycle HOC... See the code for component to see how I am using the HOC with my components. Is there a way to avoid needing to describe the PropTypes for the HOC wrapping: Component: import React from 'react'; import…
2
votes
1 answer

python object composition and multiprocessing

If I implement object composition with getattr and pass the object into a new process, I get an RecursionError from getattr. Here is an example: from multiprocessing import Pool class Bar: def __init__(self, bar): self.bar = bar class…
2
votes
2 answers

a realistic usage of composition in python

i have been reading about composition in python and after following some articles and answers here on stackoverlow i think i am able to get what it is and how it is implemented. but one question to which i am unable to find answer is why…
anekix
  • 2,393
  • 2
  • 30
  • 57
2
votes
4 answers

Composed object initialization

This is somewhat of a broad question that seems to have no one true answer. I've been confused about the initialization of composed objects for quite some time. I've been formally taught to supply getters and setters for all member data and to favor…
2
votes
1 answer

Switch statements for Object composition in Python

I'm having problems designing a (Python) switch pattern that works well with object composition. More specifically I want to create a function that gets an 'entity_id' as argument (+other relevant arguments), creates an object and matching…
2
votes
1 answer

Enterprise Architect code generation - compose - membervariable as array

I've this class diagram and the multiplicity 1 : 0..* The generated code from this model: public class TestKlasseB { public TestKlasseB(){ } ~TestKlasseB(){ } }//end TestKlasseB public class TestKlasseA { public…