Wednesday 20 May 2015

Favor Composition Over Inheritance

Composition Over Inheritance

What is Inheritance?
In Inheritance, one class can acquire all the members of another class except private members.
Inheritance means one class (Subclass) extends the functionality of another class (Super Class).

Inheritance is IS-A Relationship between two or more components

What is Composition?
Composition means a class (which wants to implement a functionality) uses other classes (that implement the desired functionality) as instance reference variables. Main candidate class does not implement that existing functionality. It uses and delegates to other classes (who are already implemented desired functionality and who are responsible to provide that functionality).

Composition is HAS-A Relationship between two or more components

Common Advantages of Inheritance and Composition:
1. Code Reusability.
2. Reduces code duplication.

Advantages of Inheritance:
1. Ease of code change with Dynamic Binding and Polymorphism
2. Ease to add new subclasses
3. As Inheritance uses single method call, it may have some performance improvement as compared to Composition uses delegation approach.

Advantages of Composition:
1. Easier-to-change code

Major Drawbacks of Inheritance:
1. Breaks Encapsulation principle
2. When Inheritance levels increases, it is very tough and hard to maintain 
    and create objects.

Major Drawbacks of Composition:
1. As Composition uses delegation approach, it may have a performance cost as compared to inheritance's single invocation of an inherited superclass method implementation.

What is Dynamic Binding?
JVM uses Dynamic Binding to decide which method should be invoked at runtime based on the class of the object.

What is Polymorphism?
Polymorphism is a technique of an object to take on many forms. 
Polymorphism means we can use a reference of a superclass to hold a reference to an object whose class is the subclass or any of its subclasses.


Reasons to Use "Favor Composition Over Inheritance" Design Principle:

1. Flexibility 
Inheritance resolves the required functionality at compile-time where as Composition does this at run-time. So we can write more flexibility code with Composition. Major Advantage of Composition is easier-to-change code

2. Ease of Testability
In Composition we use other dependent objects references as instance variables so that we can mock them and test our class functionality very easily.

3. Does NOT break Encapsulation
If we use inheritance to reuse and extend existing functionality, then there are some cases to break this functionality in future. 

For instance, our subclass method is reusing super class method to extend the functionality. If we change super class method behavior, then it will also change the subclass functionality. Inheritance provides weak encapsulation .

If we use Composition over Inheritance, there is no breakup of the functionality with future changes.

4.Java does NOT support Multiple Inheritance
As Java does not support Multiple Inheritance, it is better to use Composition to utilize more than one class functionality.

5. Well tested Design Patterns uses this Design Principle
Most of the well defined and tested Design Patterns follow this Design Principle.

The following design patterns follows this "Favor Composition Over Inheritance" Object-Oriented Design Principle
1. Strategy Design Pattern
2. Decorator Design Pattern
3. Bridge Design Pattern
4. Adapter Design Pattern
5. State Design Pattern
6. Delegate Design Pattern
7. Façade Design Pattern
8. Mediator Design Pattern
9. Manager Design Pattern
10. Operator Design Pattern
11. Command Design Pattern




No comments:

Post a Comment