Hubbry Logo
search
logo

Twin pattern

logo
Community Hub0 Subscribers
Write something...
Be the first to start a discussion here.
Be the first to start a discussion here.
See all
Twin pattern

In software engineering, the Twin pattern is a software design pattern that allows developers to model multiple inheritance in programming languages that do not support multiple inheritance. This pattern avoids many of the problems with multiple inheritance.

Instead of having a single class which is derived from two super-classes, have two separate sub-classes each derived from one of the two super-classes. These two sub-classes are closely coupled, so, both can be viewed as a Twin object having two ends.

The twin pattern can be used:

There will be two or more parent classes which are used to be inherited. There will be sub-classes each of which is derived from one of the super-classes. The sub-classes are mutually linked via fields, and each sub-class may override the methods inherited from the super-class. New methods and fields are usually declared in one sub-class.

The following diagram shows the typical structure of multiple inheritance:

The following diagram shows the Twin pattern structure after replacing the previous multiple inheritance structure:

Each child class is responsible for the protocol inherited from its parent. It handles the messages from this protocol and forwards other messages to its partner class.

Clients of the twin pattern reference one of the twin objects directly and the other via its twin field.

See all
User Avatar
No comments yet.