OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: Prefer Composition Over Inheritance
View page
Source of version: 2
(current)
{maketoc} !!What is this? "__Composition__ - Functionality of an object is made up of an aggregate of different classes. In practice, this means holding a pointer to another class to which work is deferred. __Inheritance__ - Functionality of an object is made up of it's own functionality plus functionality from its parent classes." (from: [http://blogs.msdn.com/steverowe/archive/2008/04/28/prefer-composition-over-inheritance.aspx|Steve Rowe's Blog]) More detailed description with examples (Java and UML): [http://www.artima.com/designtechniques/compoinh.html|Artima development site] !!Why? "For most non-trivial problems, there will be similar code needed by multiple classes. It is not a wise idea to put the same code in more than one place (a topic for another day). There are two strategies in object-oriented programming which attempt to solve the problem of duplicate code. The one most popular in the early days was inheritance. Shared functionality was implemented in a base class which allowed each child class to inherit that functionality. A child would just not implement foo() and the parent would do the work. This works, but it is not very flexible. Suppose that the shared functionality is some kind of encryption algorithm. Each child class will only inherit from one base class. What if there are a few different encryption algorithms? It would be possible to have multiple base classes, say AESEncryptionBase and DESEncryptionBase, but this necessitates multiple copies of the child classes--one for each base class. With more than 2 base classes, this become untenable. It also becomes very difficult to change out the encryption routine at runtime. Doing so means creating a new object and copying the contents of the old object to it." (From: [http://blogs.msdn.com/steverowe/archive/2008/04/28/prefer-composition-over-inheritance.aspx|Steve Rowe's Blog]) * fewer code ("It would be possible to have multiple base classes, say AESEncryptionBase and DESEncryptionBase, but this necessitates multiple copies of the child classes--one for each base class.") * change the routine more easily: if you use a specified Interface (here: to the encryption-classes) you can change the routine whenever you want !!Links * [http://blogs.msdn.com/steverowe/archive/2008/04/28/prefer-composition-over-inheritance.aspx|Steve Rowe's Blog] * [http://www.artima.com/designtechniques/compoinh.html|Artima development site]
Search by Tags
Search Wiki by Freetags
Latest Changes
One Function Ogre
IDE Eclipse
FMOD SoundManager
HDRlib
Building Ogre V2 with CMake
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
...more
Search
Find
Advanced
Search Help
Online Users
136 online users