Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composition vs. Delegation

Tags:

Is there any difference in terms of implementation as how a composition design can be different from delegation. For example the code below seems to be doing delegation since the user cannot access the composed object (i.e "a") without using b. Hence, the user would need to invoke interfaces of class b and then "class b" invoke appropriate interfaces of "class a" making it delegation. Does this make sense ?

Class A {
friend class B;
private: 
A(){}; //dont want user to instantiate this class object since it wont sense without any context. Just like a room with no house.
void PrintStructure(){};
};

Class B{
public:
void PrintStructure(){a.PrintStructure();} //delegate

private:
A a; //composition
};
like image 242
Frank Q. Avatar asked Jan 26 '10 02:01

Frank Q.


People also ask

What is object composition and delegation?

Delegation: When my object uses another object's functionality as is without changing it. Composition: My object consists of other objects which in turn cannot exist after my object is destroyed-garbage collected. Aggregation: My object consists of other objects which can live even after my object is destroyed.

What is the difference between composition and inheritance?

Inheritance and composition are two programming techniques developers use to establish relationships between classes and objects. Whereas inheritance derives one class from another, composition defines a class as the sum of its parts.

What is the difference between delegation and delegation?

In this case, "delegate" can appear both as a noun and as a verb, whereas "delegation" can only function as a noun. Both words are related to the concept of sending a representative individual or group of people somewhere. But we'll explain these meanings in more detail in the next sections. When do we use "delegate"?

Why is delegation better than inheritance?

The primary advantage of delegation is run-time flexibility – the delegate can easily be changed at run-time. But unlike inheritance, delegation is not directly supported by most popular object-oriented languages, and it doesn't facilitate dynamic polymorphism.

What is the difference between delegation and composition and aggregation?

Delegation: When my object uses another object's functionality as is without changing it. Composition: My object consists of other objects which in turn cannot exist after my object is destroyed-garbage collected. Aggregation: My object consists...

How do you use composition with delegation in Java?

When you use composition with delegation as illustrated in Figure 4.7, you need to explicitly declare and implement a ClassB function like foo () which is intended to pass off the call to the ClassA member method foo (). This is different from inheritance, where a child class automatically gets the methods of the parent class.

What is the difference between inheritance and object delegation?

Object Delegation means using the object of another class as a class member of another class. It is known as object delegation. Delegation can be an alternative to inheritance, but in an inheritance, there is an i-s a relationship, but in the delegation, there is no inheritance relationship between the classes.

What is the difference between internal classes and object delegation?

The internal classes can be changed without any side effects and changes can be handled internally. Object Delegation means using the object of another class as a class member of another class. It is known as object delegation.


2 Answers

The term "composition" is usually used in terms of object modelling as an expression of a "has-a" relationship and is a form of association (another being aggregation). This is usually contrasted with "inheritance" ("is-a" relationship). So:

What's the difference between composition and aggregation? Composition implies that the child cannot exist without the context of the parent.

For example, a House has one or more Rooms. That's a composition relationship. Delete the house and the rooms also cease to exist. A House also has a number of occupants, being instances of Person. That's an aggregation relationship because those people exist outside of the context of that house.

Delegation is nothing more than an implementation detail. A class has a public interface that describes its state and behaviour. How that is implemented is irrelevant. It could delegate to other objects or not.

You'll note that both A and B from your example have the same external interface. It's more common to do something like this:

// this represents an interface
class A {
public:
  virtual void printStructure() = 0;
}

with concrete classes:

class ConcreteA : A {
public:
  virtual void printStructure() { ... }
}

and

class DelegateA : A {
public:
  DelegateA(A& a) { this.a = a; }
  virtual void printStructure() { a.printStructure(); }
private:
  A a;
}

Excuse my probably C++ syntax errors. I'm a little rusty.

like image 80
cletus Avatar answered Oct 17 '22 14:10

cletus


There are a couple of differences I see:

  • Delegation involves re-exporting methods; in a composition relationship, the inner objects methods may be used only privately and not re-exposed.
  • Composition usually implies some kind of ownership semantics with implications for object lifecycle; the parent object "owns" the child and the child doesn't have much reason to exist on its own. Delegation does not have this implication.

The code you show uses delegation and association; the association may be composition, but it's difficult to tell without broader context or more information about the objects (it can be quite subtle and subjective when an association becomes a composition).

like image 28
Michael Ekstrand Avatar answered Oct 17 '22 12:10

Michael Ekstrand