Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [composition]

The tag has no usage guidance.

8
votes
9answers
585 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
1answer
137 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 ...
1
vote
4answers
100 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 ...
0
votes
1answer
71 views

Inheritance or composition for a more advanced implementation of a class?

So i have a class called VirtualMouse, it is used to perform mouse actions such as moving and clicking. public class VirtualMouse { public VirtualMouse() { } public void ...
4
votes
3answers
306 views

How to reach the parent object?

I have a parent object that has some other objects as fields. The parent object fully owns these fields: they can be declared as fields of the parent object (MyPart part), directly, not as references. ...
1
vote
2answers
84 views

Composition - legal class diagram

I wanted to ask if this is a valid class diagram: I tend to think not, because it forces a certain wheel to belong to two vehicles at the same time (because of the 1 multiplicity). If the ...
2
votes
1answer
41 views

Decomposition by “role” or by “usage” - is that a thing?

I have a mail_queue table that is written to by web requests then worked on by a worker to send the mails. I've now had a moment of enlightenment re. what I was not comfortable with in the design. ...
-1
votes
1answer
45 views

How to model parent/child composite when the child is related to another class

I don't know which is the correct way to model this requirement in OOD: A device has many properties, each property has exactly one device parent. There are a set of procedures and a specific ...
4
votes
2answers
356 views

Does composition increase or decrease the coupling

We know that composition is a synonym of loose coupling. From here I read coupling becomes stronger between the classes A and B when: A has an attribute that refers to (is of type) B. To ...
0
votes
3answers
80 views

Abstract responsibility from caller without introducing complexity

I have two classes: public class Child { public List<Vector2> localPoints; public List<Vector2> localEdges; } public class Parent { public List<Child> children; ...
2
votes
6answers
856 views

Should I still prefer composition over inheritance if the child classes need BOTH the parent interface AND its class properties?

According to Why should I prefer composition over inheritance?, I should prefer composition over inheritance. But what if I need to access the interface and class member in generic way? For example, I ...
1
vote
4answers
2k views

Why the industry prefer/use composition over inheritance? [duplicate]

I was having a discussion with few friends about inheritance and composition and what I learnt from that discussion is that the use of inheritance more or less condemned in industry nowadays and ...
0
votes
0answers
26 views

How to organize classes which should reflect different versions of standard?

I have some standard which changes in time. Let's say I have std_v1, std_v2 and std_v3. Those standards have a lot of common parts with some little differences, possible additions and deletions. On ...
1
vote
3answers
160 views

Strategy Pattern and inheriting boilerplate functionality

I've read that when using the Strategy Pattern, you should stick to composition rather than inheritance. But does this mean that any inheritance at all is bad practice? If there is functionality to be ...
0
votes
2answers
119 views

Version checking when reading deeply nested structures

I need to open a game save file, read it, maybe modify it and then save it. The structure of the save is quite simple: it is an object with some members, some of which are data, while others are ...
36
votes
3answers
10k views

Is “composition over inheritance” violating “dry principle”?

For example, consider I have a class for other classes to extend: public class LoginPage { public String userId; public String session; public boolean checkSessionValid() { } } and ...
4
votes
2answers
142 views

Composition and calling part methods

What is the right concept of calling methods of parts in composition? I have a class class Body{ Arm arm; } class Arm{ public void waveArm(){} Finger finger } class Finger{ public void ...
2
votes
2answers
157 views

Is Chaining of Composition the right approach?

I'm working on some code where the structure looks like: Class A { Client client; B b; void process(request) { data = fetchData(client, data); b.process(data); } } class B { C c; ...
3
votes
1answer
200 views

To inherit or to override?

Imagine that I am writing a game where tanks fight with each other. A generic Tank class is created and has the method fire() which fires a cannon, looks like this Tank::fire() { /* fires a cannon */ ...
0
votes
1answer
58 views

How to represent self-composed implementations in inheritance hierarchies

Background: My company is working with camera systems. In the beginning we built our application around different models/setups of them, which i.e. consisted of different cameras or procedures to ...
6
votes
2answers
570 views

OOP without Inheritance [closed]

I'm making a language, and I'm considering disallowing inheritance. To be clear, this means: class A {} //Valid interface B{} //Valid class A2 extends A {} //Invalid class B2 implements B {} //...
-1
votes
1answer
92 views

What would be the best option in this scenario?

I have a statement. I know the ups and downs of both Composition and Inheritance, but I am unable to realize that what would be the best option the given scenario. SCENARIO: A Software company is ...
0
votes
1answer
220 views

Convenience vs Decoupling/Composition

Let's say I have a small CD burning library written in Python, with a CD class and a Burner interface + implementation classes: # Module cd.py class CD: def __init__(self): self.folders = [] ...
13
votes
8answers
4k views

“Prefer composition over inheritance” - Is the only reason to defend against signature changes?

This page advocates composition over inheritance with the following argument (rephrased it in my words): A change in the signature of a method of the superclass (which hasn't been overridden in ...
2
votes
4answers
898 views

Composition over Inheritance, why not both?

I have this out of context scenario, where what I think is good practices leaves me in a situation of both implementing an interface, and using composition to do the implementation. Imagine the ...
3
votes
2answers
249 views

Should I nest repositories like Russian dolls?

tl;dr Can I have a repository depend on another repository, where they both implement the same interface? I am working on a website analytics system that involves getting data from different sources. ...
2
votes
1answer
618 views

Dependecy Injection and Composition Root

Just recently started to grasp on MVC and design patterns. (back to school at 34 ;) ) I was asked by my teacher to design a two part application. It's a recipe website that as an admin WinForms app. ...
10
votes
1answer
516 views

Is there a situation where it would be better to use weak references instead of simple composition?

Although the Java docs specify, that Weak references are primarily for canonicalizing mappings, you will find many, many, many people on the internet stating, that the WeakHashMap is perfect for ...
4
votes
2answers
546 views

Java8: why two composition methods: andThen and compose?

As a beginner in both java8 and functional programming, I think I'm missing something when reading about function composition since I cannot find a reason why there are two methods that do this, ...
5
votes
2answers
134 views

Would you use object proxy, extend the class, if else conditions or make a duplicate class for supporting two different API's?

This is not an easy one but here goes. I'm working on adapting a JavaScript project (ACE Editor) to support two different targets but maybe more. Any way I look at it, it looks like a large task and ...
5
votes
5answers
479 views

Should I encapsulate an object inside another object as methods or just access it directly?

Suppose I have a class A: public class A{ public void a(){ } } and class B use A: public class B{ private A a; } Should I encapsulate A in B: public class B{ private A a; ...
7
votes
3answers
362 views

Composition of two classes with common inheritance root

Hope your day is going well. I want to make the following composition of two objects with the same inheritance root: There will be 3 basic operations on the Wallet class: add, getCards and ...
7
votes
2answers
275 views

Gateway Pattern and Composition

I often write database intensive applications and discovered the gateway pattern which seemed to fit my needs. My problem now is that many of my models are compositioned of other models. For example ...
2
votes
1answer
176 views

Is this an example of Composition?

The following code is given: class Warren { private const int MaxRabbitsInWarren = 99; private Rabbit[] Rabbits; private int RabbitCount = 0; private int PeriodsRun = 0; private bool ...
4
votes
2answers
173 views

Which pattern should I use for building up a character?

I have an application that lets users create their own character from clothing like hat, pants and facial features, like beard and eyes. I'm using a canvas to display the built up character. The order ...
0
votes
1answer
160 views

Is this an example of Composition or Aggregation?

I understand the concept behind Composition ('has a') where the contained class is destroyed upon termination of the container class. Likewise, I get Aggregation which is a 'looser' relationship ...
2
votes
5answers
150 views

Inheritence structure to allow unknown types

Let's say I have a client that is consuming Vehicle objects sent from a server. I have two types of vehicles: Car and Truck. Some Cars can be inHybridMode but not all of them. The server currently ...
1
vote
1answer
426 views

Design Pattern or suggestion for implementing

The Problem A problem I keep running into again and again in C# is how to treat a class of things as the same in some contexts, but as different in different contexts. I have found some ways of ...
2
votes
0answers
117 views

Interface Methods vs Data Objects

Building a UI editor for Android and basically there are interfaces such as this: interface Item { fun decorate() fun translate(x: Int, y: Int) fun rotate(rotation: Float) fun scale(...
1
vote
1answer
88 views

Code reuse on function write objects data to file

I have a few base, derived classes and need to dump their data to file. The difference is only a field, but I don't know how to reuse code of base class. Sorry if noob question :p Those classes look ...
0
votes
1answer
112 views

Abstract class vs relation between classes

Searching for simplest solution where a User can order an item. If someone purchases an item he will have one address. Which then is the invoice and shipping address. But he could add another address ...
2
votes
3answers
140 views

Base Class vs Common Member Class

Imagine I have two classes like this... public class SingleplayerGameSetup { public int Seed { get; private set; } public int AiNations { get; private set; } } public class ...
7
votes
5answers
711 views

Is “too much methods” a valid reason to break the “composition over inheritance” rule?

I usually prefer to follow "composition over inheritance" rule, until I start stepping in the world of Java GUI. In my experience, for adding custom requirements in a GUI components, for example, add ...
2
votes
1answer
143 views

Can composition be used in unit/integration testing over inheritance

In larger projects, there can often be a group of tests that test the functionality of related classes. These tests often share a lot of setup and preparation functionality. What happens then is ...
1
vote
1answer
358 views

Should we “prefer composition over multiple interface”?

Should we avoid multiple interface if possible? Because I think at most cases a class with multiple interfaces can be replaced with another version with composition and single interface only e.g.: ...
2
votes
2answers
2k views

Java: Composition of classes implementing same interface

Let's consider an example wherein I have to model the following: Class to schedule exams for a Student, lets call it StudentExamScheduler Class to schedule exam for a Class. Let's call it ...
0
votes
1answer
296 views

Should I force “composition over inheritance” rule to class members?

As I know, according to "composition over inheritance" rule, we should avoid reusing a method by inheritance, but how about class members? Suppose I have parent and child classes like the following: ...
3
votes
2answers
962 views

Composition of Data Transfer Objects

Let's say I want to create a shop/order system. I'll have an Order DTO to which I'll map the data from a request and results from the database. The object will consist of an order number, a customer ...
2
votes
1answer
141 views

Term for the opposite of the functional Compose operator

In functional programming, given a function f and a function g we can define a function . (compose): let (.) f g = fun x -> g (f x) Is there a term for the function which applies arguments in the ...
0
votes
1answer
425 views

Separate interfaces for dependency data providers?

I am attempting to refactor a component of a large project, which currently has a lot of dependencies to global state of the project environment. The goal (for my taste) is a "SOLID" architecture, ...