composition

Prefer composition over inheritance?

Why prefer composition over inheritance? What trade-offs are there for each approach? When should you choose inheritance over composition? ...

How does a game engine that models objects as "collections of components" work at runtime?

I'm writing a lightweight game engine and while doing some research for it I've come across a number of compelling articles advocating the implementation of Game Objects through a "collection of components" model rather than an "inheiritance from concrete classes" model. There are lots of advantages: objects can be composed using data...

Object Oriented Best Practices - Inheritance v Composition v Interfaces

I want to ask a question about how you would approach a simple object-oriented design problem. I have a few ideas of my own about what the best way of tackling this scenario, but I would be interested in hearing some opinions from the Stack Overflow community. Links to relevant online articles are also appreciated. I'm using C#, but the ...

Meta-composition during music performances

A couple of weeks ago, my piano teacher and I were bouncing ideas off of each other concerning meta-composing music software. The idea was this: There is a system taking midi input from a bunch of instruments, and pushes output to the speakers and lights. The software running on this system analyzes the midi data it's getting, and deter...

Would syntax for composition be a useful addition to Java?

First off, I know next to nothing about language theory, and I barely know any other languages except Java, but I had an idea that I think would be cool, but I need you guys to tell me: a: why it sucks b: how language x has had that for years c: how my mind sucks d: all of the above The idea would give composition the same ease of code ...

How do I use composition with inheritance?

I'm going to try to ask my question in the context of a simple example... Let's say I have an abstract base class Car. Car has-a basic Engine object. I have a method StartEngine() in the abstract Car class that delegates the starting of the engine to the Engine object. How do I allow subclasses of Car (like Ferrari) to declare the En...

How to pass method result as parameter to base class constructor in C++?

I've trying to achieve something like this: class Base { public: Base(string S) { ... }; } class Derived: Base { public: int foo; string bar() { return stringof(foo); // actually, something more complex }; Derived(int f) : foo(f), Base(bar()) { }; } Now, this doesn't work as I want, because bar() is ca...

Haskell: How to compose `not` with a function of arbitrary arity?

When I have some function of type like f :: (Ord a) => a -> a -> Bool f a b = a > b I should like make function which wrap this function with not. e.g. make function like this g :: (Ord a) => a -> a -> Bool g a b = not $ f a b I can make combinator like n f = (\a -> \b -> not $ f a b) But I don't know how. *Main> let n f = (\a...

A simple example of dojo widget composition using InlineEditBox ?

How can I use dojo 1.2 to : a. define my own widget which inherits from an existing widget (say, the dijit.InlineEditBox widget) b. programmatically insert a second widget (say a dijit.form.TextArea widget) so that it is the inline editable widget. I can see how this sort of stuff works with a template html file, but if I want to do ...

UI composition in ASP.NET MVC

How would you go about supporting external composable parts in an ASP.NET MVC view? What do I mean by this? Think either "login box on every page" or "iGoogle". It's stuff that needs to be in certain places that is external to each controller/view. One approach at this would be adding components in the view like so: <% foreach (var c...

Liskov Substition and Composition

Let say I have a class like this: public sealed class Foo { public void Bar { // Do Bar Stuff } } And I want to extend it to add something beyond what an extension method could do....My only option is composition: public class SuperFoo { private Foo _internalFoo; public SuperFoo() { _internalFo...

Best way to embed Login Form in Zend Framework pages

What is the best way to embed Login Form into several pages in Zend Framework? Currently I have two controllers, LoginController for separate login form and IndexController for actions on index page. I need to include Login Form into index page to let users log in both from the front page and from Login page. My current solution is to...

Java composition question

A question about composition and object orientation: I am trying to implement more features for a class (Java TreeMap as an example). public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, Serializable Using composition is the way to go on this, so I would have to create first a reusable ...

Composition vs Inheritance for Equality & Hashcode providers

When comparing entities and aggregate roots I use an ABC, which I borrowed from Oren Eini: Generic Entity Equality. For value objects I was equally ingenious. I used Jimmy Bogard’s Value Object ABC: Generic Value Object Equality Now my question is; should I be favouring inheriting these ABCs or should I perhaps be using the generic equa...

"Has a" vs "Is a" - code smells for deciding

I wrote this yesterday, in a class Foo inheriting from Bar: public override void AddItem(double a, int b) { //Code smell? throw new NotImplementedException("This method not usable for Foo items"); } Wondered subsequently if this is a possible indication that I should be using a Bar, rather than inheriting from it. What other...

[PHP] How to combine words of a sentence to composed terms?

Hello! I have a sentence, for example John Doe moved to New York last year. Now I split the sentence into the single words and I get: array('John', 'Doe', 'moved', 'to', 'New', 'York', 'last', 'year') That's quite easy. But then I want to combine the single words to get all the composed terms. It doesn't if the composed term...

inheritance vs. composition for testability

While designing my objects I find composition to be a better choice from the perspective of testability. The reason being, I can mock parts of the composition structure if I need to, while running unit tests. This is not possible if I have an inheritance hierarchy. I would like to know if others have also found this to be a reason to p...

Terminology - parts of a composite relationship

Assume I have a composite relationship, say a Customer having a collection of Orders (and assuming an Order cannot exist without an "owning" Customer.) So, I'm not talking about aggregation. What terms are used to describe the roles in this relationship? I might say the Customer is the "owner" of an Order and maybe the Order is "owned"...

How do you model a simple composition relationship

Can someone help me understand how best to model a composition relationship? If for instance I have a student whom can have many schedules, I would create roughly: class Student { prop long Pk { get; set; } prop string Name { get; set; } prop List<Schedule> Schedules { get; set; } } class Schedule { prop string Semester { get;...

Decorator Pattern Using Composition Instead of Inheritance

My previous understanding of the decorator pattern was that you inherit Window with WindowDecorator, then in the overridden methods, do some additional work before calling the Window's implementation of said methods. Similar to the following: public class Window { public virtual void Open() { // Open the window } } pu...