Favor composition over inheritance

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Ebybymic (Talk); changed back to last version by Nelson Shaw)
 
Line 1: Line 1:
----
 
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 
----
 
=[http://oqagacyti.co.cc Under Construction! Please Visit Reserve Page. Page Will Be Available Shortly]=
 
----
 
=[http://oqagacyti.co.cc CLICK HERE]=
 
----
 
</div>
 
 
Composition should be used when possible because composition minimizes coupling, and therefore increases 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.  Inheritance should only be used when necessary, or when the benefits of inheritance can be fully utilized.
 
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 &quot;plug-in&quot; 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.
+
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.

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

Personal tools