Composition over inheritance

Composition over inheritance

Definition

Composition over inheritance: is the principle that classes should achieve polymorphic behavior and code reuse by their composition rather than inheritance from a base or parent class.

Composition : containing instances of other classes that implement the desired functionality

mixin: a class that contains methods for use by other classes without having to be the parent class of those other classes. Mixins are sometimes described as being “included” rather than “inherited”.

Why

Drawbacks

Avoiding Drawbacks

This drawback can be avoided by using traits, mixins, (type) embedding, or protocol extensions.

Some languages provide specific means to mitigate this:

ref

Powered by Jekyll and Theme by solid

本站总访问量