java – Difference between delegation and composition?

Question:

What's the difference between delegation and composition?
-Delegation when an instance of another class is created in a class and methods are called?

public class A{  
    int  a;   
    void b(){}   
}

class B{   
    B() {
        A a = new A();   
        a.b();   
    }    
}

-Composition is when an instance of another is created in one class and fields in an instance of another class can dynamically change?

public class A{     
    int a=5;     
    void b(){}    
}

class B{   
    B(){  
        A a = new A();     
        a.a=10; 
    }     
}

Answer:

These are different concepts.

Delegation is when a class "transfers" part of the work to other classes.

For example, the development manager, having received an order for a new feature, delegates this task to one of his subordinates.

An example of delegation in programming is when a class that performs a complex algorithm logs its actions not independently (by opening a file and appending to it), but using the Logger class, calling its methods in the right places.

Usually, delegation is opposed to the implementation of something in the same class. For example, if you have an "omnipotent" class, then it is better to divide it into several classes, each of which will have its own area of ​​responsibility. The main class will only deal with dispatching calls between these classes.

Check out the article on the Wiki .

Composition is when the relationship "part – whole" (has a) is clearly traced between the objects being modeled.

For example, when modeling a car, it is natural to assume that the engine is part of the car. When you program this model, you express it by making an object of the Engine class a member of the Auto class .

Compositions usually contrast aggregation and association. Aggregation is similar to composition in that it is also a part-whole relationship, but there is no ownership relationship between objects. For example, a "group of students" – a student is part of a group, but a student can exist outside the group. That is, the group destructor should not "kill" student objects. An association expresses any relationship between objects that can call each other's methods.

That is, delegation can be arranged having both an association relationship and a composition relationship between objects.

Compositions are also opposed to inheritance. But in this case, composition and delegation are synonymous. In the case of inheritance, we move common methods to the base class Base , and various implementations to Derived1 classes Derived1 , Derived2 . By instantiating one of the Derived classes, we get the functionality we need from the object.

But the same can be done in a different way. In the Base class, we still leave the general methods, and add a reference (pointer) (that is, we aggregate a pointer to the implementation) to the IRealization helper method IRealization . Helper methods are implemented differently in the classes Realization1 , Realization2 , which inherit * the IRealization interface. Now, depending on the object of which of the Realization classes the Base object will point to, we will receive different functionality. Base delegates its IRealization to IRealization .

PS. there was a good article on Habré about the last paragraph, but I didn't find it. Therefore, I am sending you to the original source of that article – a book on Freeman Design Patterns.

Scroll to Top