3
votes
1answer
182 views

How to maintain encapsulation with composition in C++?

I am designing a class Master that is composed from multiple other classes, A, Base, C and D. These four classes have absolutely no use outside of Master and are meant to split up its functionality ...
1
vote
2answers
79 views

Inheritance vs 'specification' by fields

I'm currently reading 'Head first design patterns', and already the first chapter threw some questions towards me. This chapter introduces the 'prefer composition over inheritance' design principle, ...
1
vote
2answers
125 views

Alternatives to inheritance on complex structures?

I'm working on an ASP.NET Webforms project, using the MVP Pattern (which the presenters as you may know use a Page Controller pattern). I'm given a rather complex inheritance chain for my controllers. ...
3
votes
3answers
140 views

Why should one subclass instead of composite when using the Factory Method design pattern?

I'm currently learning about design patterns. I learned about the Factory Method pattern. This pattern means that in order to implement a factory to create objects, one should subclass the class that ...
3
votes
1answer
173 views

Inheritance in test classes

I have an interface Serializer with methods serialize and isSerializerFor. I created a first implementation of this using TDD, and ended up with a nice clean test case fully covering a nice clean ...
1
vote
1answer
205 views

Internal exposure of implementation details on inheritance

I'm reading the "Effective Java" book which suggests to favor composition over inheritance. The example it gives shows something like this: public class InstrumentedHashSet<E> extends ...
7
votes
2answers
478 views

How do I avoid writing lots of pass-through functions in a wrapper?

I have a class, which wraps another class of a common base type. Because the base type interface is quite large this involves writing a lot of pass-through functions. I am looking for a way to avoid ...
6
votes
4answers
810 views

Looking for a real-world example illustrating that composition can be superior to inheritance

I watched a bunch of lectures on Clojure and functional programming by Rich Hickey as well as some of the SICP lectures, and I am sold on many concepts of functional programming. I incorporated some ...
0
votes
5answers
1k views

Inheritance vs composition in this example

I'm wondering about the differences between inheritance and composition examined with concrete code relevant arguments. In particular my example was Inheritance: class Do: def do(self): ...
3
votes
7answers
902 views

Inheritance using non-abstract base class

This post is based on the question http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance/11758048#comment15634305_11758048. Some people said - check whether there is “is-a” ...
3
votes
1answer
514 views

Composition vs. Inheritance [duplicate]

Here's what is given: public interface Request {} // there are 20 subclasses of Request public class CreateUserRequest implements Request { @NotEmpty public String userName; } // request ...
26
votes
4answers
12k views

Why should I prefer composition over inheritance?

I always read that composition is to be preferred over inheritance. A blog post on unlike minds, for example, advocates using composition over inheritance, but I can't see how polymorphism is ...
0
votes
2answers
245 views

Inheritance versus Composition in a business application

I have a training management and tracking system, with a high level structure as follows: We have a Role1, e.g. Manager, Shift-boss, miner, etc. and a Candidate, training for that Role. The role has ...
3
votes
1answer
325 views

Is it possible to refactor inheritance to composition when virtual methods are called inside the base class?

Let's say I have a class called Country and two subclasses called AI and Player. The Country class has a number of virtual methods to allow player-specific or AI-specific behavior. I want to make it ...
9
votes
5answers
1k views

How do we know to favour composition over generalisation is always the right choice?

Whether an object physically exists or not, we can choose to model it in different ways. We could arbitarily use generalisation or composition in many cases. However, the GoF principle of "favour ...
71
votes
14answers
18k views

Where does this concept of “favor composition over inheritance” come from?

In the last few months, the mantra "favor composition over inheritance" seems to have sprung up out of nowhere and become almost some sort of meme within the programming community. And every time I ...
0
votes
5answers
1k views

Why does the use of interface-based programming appear to be limited to behaviour?

I have been doing a little thinking about inheritance vs. realization vs. composition. I am not about to post the whole detail here. So I was wondering, when we are not talking about creating ...
36
votes
9answers
4k views

Code Smell: Inheritance Abuse [duplicate]

It's been generally accepted in the OO community that one should "favor composition over inheritance". On the other hand, inheritance does provide both polymorphism and a straightforward, terse way of ...