Composite reuse principle
Encyclopedia
Composition over inheritance in object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 is a technique by which classes may achieve polymorphic behavior and code reuse by containing other classes
Object composition
In computer science, object composition is a way to combine simple objects or data types into more complex ones...

 which implement the desired functionality instead of through inheritance
Inheritance (computer science)
In object-oriented programming , inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support...

.

This technique is also referred to as "Composite Reuse Principle".

Basics

An implementation of composition over inheritance typically begins with the creation of various interfaces representing the behaviors which the system must exhibit. The use of interfaces allows this technique to support the polymorphic behavior that is so valuable in object-oriented programming. Classes implementing the identified interfaces are built and added to Business Domain classes as needed. Thus system behaviors are realized without inheritance. In fact, business domain classes may all be base classes without any inheritance at all. Alternative implementation of system behaviors are accomplished by providing another class which implements the desired behavior interface. Any business domain class that contains a reference to the interface can easily support any implementation of that interface and the choice can even be delayed until run time.

Inheritance


class Object {
public:
virtual void update {} ;
virtual void draw {} ;
virtual void collide(Object objects[]) {} ;
};
class Visible : public Object {
public:
virtual void draw { /* draw model at position of this object*/ };
private:
Model* model;
};
class Solid : public Object {
public:
virtual void collide(Object objects[]) { /*check and react to collisions with objects */ };
};
class Movable : public Object {
public:
virtual void update { /* update position */ };
};

Then we have concrete classes:
  • class Player - which is Solid, Movable and Visible
  • class Cloud - which is Movable and Visible, but not Solid
  • class Building - which is Solid and Visible, but not Movable
  • class Trap - which is Solid and not Visible nor Movable

Using inheritance we either have to do multiple inheritance, which leads to the diamond problem
Diamond problem
In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C...

, or make classes like VisibleAndSolid, VisibleAndMovable, VisibleAndSolidAndMovable, etc. for every needed combination, which leads to a large amount of repetitive code.

Composition (and a little inheritance)


class Object {
public:
Object(VisibilityDelegate* v, UpdateDelegate *u, CollisionDelegate *c):_v(v),_u(u),_c(c) {};

void update { _u->update; };
void draw { _v->draw; };
void collide(Object objects[]) { _c->collide(objects); };
private:
VisibilityDelegate* _v;
UpdateDelegate *_u;
CollisionDelegate *_c;
};

class VisibilityDelegate {
public:
virtual void draw = 0;
};
class Invisible : public VisibilityDelegate {
public:
virtual void draw {};
};
class Model : public VisibilityDelegate {
public:
virtual void draw { /*draw model*/ };
};

class CollisionDelegate {
public:
virtual void collide(Object objects[]) = 0;
};
class Solid : public CollisionDelegate {
public:
virtual void collide(Object objects[]) { /* check collisions with object and react*/ };
};
class NotSolid : public CollisionDelegate {
public:
virtual void collide(Object objects[]) {};
};

class UpdateDelegate {
public:
virtual void update = 0;
};
class Movable : public UpdateDelegate {
public:
virtual void update { /*move object*/ };
};
class NotMovable : public UpdateDelegate {
public:
virtual void update { };
};


Then concrete classes would look like:

class Player : public Object {
public:
Player:Object(new Visible, new Movable, new Solid) {};
...
};
class Smoke : public Object {
public:
Smoke:Object(new Visible, new Movable, new NotSolid) {};
...
};

Benefits

Composition over inheritance can simplify the initial design of Business Domain
Business domain
A business domain in object oriented programming is the set of classes that represent objects in the business model being implemented. The business domain is distinguishable from the business model in that the business model is an understanding and explanation of information and behaviors in the...

 classes and provide a more stable business domain in the long term. Its advantage over inheritance is a more thorough isolation of interests than can be described by a hierarchy of descendant classes. Additionally, inheritance models are often contrived during the definition of business domain classes in order to make sense of the information in the problem domain and do not necessarily reflect the true relationship of various system objects.

Initial design is simplified by identifying system object behaviors in separate interfaces instead of creating a hierarchical relationship to distribute behaviors among business domain classes via inheritance. This approach more easily accommodates future requirements changes that would otherwise require a complete restructuring of business domain classes in the inheritance model. Additionally, it avoids problems often associated with relatively minor changes to an inheritance-based model that includes several generations of classes.

See also

  • Object-oriented design
  • Strategy pattern
    Strategy pattern
    In computer programming, the strategy pattern is a particular software design pattern, whereby algorithms can be selected at runtime. Formally speaking, the strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable...

  • State pattern
    State pattern
    The state pattern, which closely resembles Strategy Pattern, is a behavioral software design pattern, also known as the objects for states pattern. This pattern is used in computer programming to represent the state of an object. This is a clean way for an object to partially change its type at...

  • Liskov substitution principle
    Liskov substitution principle
    Substitutability is a principle in object-oriented programming. It states that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program...


Further reading

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK