Favor composition over inheritance

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(See also)
m (Reverted edits by Ebybymic (Talk); changed back to last version by Nelson Shaw)
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
The idea behind this is that having a composition relationship gives less coupling than an inheritance relationship, and increased flexibility.
+
Composition should be used when possible because composition minimizes coupling, and therefore increases flexibility.
  
If you can achieve what you need by a composition relationship then do it that way. When you have a subclass which overrides a method of the superclass, it needs to make assumptions about the state of that superclass when the method is called. It is simpler to "plug-in" behaviour of another class. Think of composition as inheriting only a small interface of the other class, whereas with inheritance you inherit the entire interface. The increased flexibility is achieved because the smaller objects that are contained in the container object can be re-used in various contexts, while adding behaviour to the container class.
+
If you can achieve what you need by a composition relationship then do it that way. Inheritance should only be used when necessary, or when the benefits of inheritance can be fully utilized.
 +
 
 +
When a class overrides a method of its super-class, it needs to make assumptions about the state of that super-class when the method is called. It is simpler to "plug-in" the behaviour of another class via a composition relationship. Think of composition as inheriting only a small part of interface of another class, whereas inheritance forces you to inherit the entire interface. Flexibility is increased because following this rule should result in a greater number of smaller objects which can be interchanged and reused in various contexts.
  
 
A good example of this in practice is the [[Strategy]] pattern.
 
A good example of this in practice is the [[Strategy]] pattern.
Line 7: Line 9:
 
== See also ==
 
== See also ==
 
* [[Design maxims]]
 
* [[Design maxims]]
 +
* [[Beware inheritance over composition]]
 +
* [[Inheritance for implementation]]
 
* [[Strategy]]
 
* [[Strategy]]
 
* [[BaseBean]]
 
* [[BaseBean]]

Latest revision as of 03:08, 25 November 2010

Composition should be used when possible because composition minimizes coupling, and therefore increases flexibility.

If you can achieve what you need by a composition relationship then do it that way. Inheritance should only be used when necessary, or when the benefits of inheritance can be fully utilized.

When a class overrides a method of its super-class, it needs to make assumptions about the state of that super-class when the method is called. It is simpler to "plug-in" the behaviour of another class via a composition relationship. Think of composition as inheriting only a small part of interface of another class, whereas inheritance forces you to inherit the entire interface. Flexibility is increased because following this rule should result in a greater number of smaller objects which can be interchanged and reused in various contexts.

A good example of this in practice is the Strategy pattern.

See also