Twin pattern
In software engineering, the Twin pattern is a software design pattern that allows to model multiple inheritance in programming languages that do not support multiple inheritance feature. This pattern avoids many of problems with multiple inheritance.[1]
Definition
Basic idea is instead of having a single class which is derived from both two super classes, two separate sub-classes each of which are derived from the each of two super-classes,respectively, can be created. These two sub-classes are closely coupled, so, both can be viewed as a Twin object having two ends.[1]
Applicability
Mainly twin pattern can be used:
- to model multiple inheritance in a laguague in which multiple inheritance is not supported
- to avoid some problems in multiple inheritance.[1]
Structure
There will be two or more parent classes which are used to be inherited. There will be sub-classes each of whice is inherited from each super-class respectively, and sub-classes are mutually linked via fields, and each sub-class may override the methods inherited from the superclass. New methods and fields are usually declared in one subclass. [1]
Following diagram shows typical multiple inheritance structure:
Following diagram shows Twin pattern structure after replacing the previous structure with multiple inheritance:
Collaborations
Each child class is responsible for the protocol inherited from its parent. It handles the messages form this protocol and forwards other messages to its partner class. [1] Cleints of the twin pattern reference one of the twin objects directly and the other via its twin field.[1] Clients that rely on the protocols of parent classes communicate with objects of the repective child class. [1]
Sample Code
Following code is a sketched implementation of a computer game board with moving balls.
class for the game board:
public class Gameboard extends Canvas {
public int width, height;
public GameItem firstItem;
…
}
code sketch for GameItem class:
public abstract class GameItem {
Gameboard board;
int posX, posY;
GameItem next;
public abstract void draw();
public abstract void click (MouseEvent e);
public abstract boolean intersects (GameItem other);
public abstract void collideWith (GameItem other);
public void check() {
GameItem x;
for (x = board.firstItem; x != null; x = x.next)
if (intersects(x)) collideWith(x);
}
public static BallItem newBall
(int posX, int posY, int radius) {//method of GameBoard
BallItem ballItem = new BallItem(posX, posY, radius);
BallThread ballThread = new BallThread();
ballItem.twin = ballThread;
ballThread.twin = ballItem;
return ballItem;
}
}
code sketch for the BallItem class:
public class BallItem extends GameItem {
BallThread twin;
int radius; int dx, dy;
boolean suspended;
public void draw() {
board.getGraphics().drawOval(posX-radius, posY-radius, 2*radius, 2*radius); }
public void move() { posX += dx; posY += dy; }
public void click() {
if (suspended) twin.resume(); else twin.suspend();
suspended = ! suspended;
}
public boolean intersects (GameItem other) {
if (other instanceof Wall)
return posX - radius <= other.posX
&& other.posX <= posX + radius
|| posY - radius <= other.posY
&& other.posY <= posY + radius;
else return false;
}
public void collideWith (GameItem other) {
Wall wall = (Wall) other;
if (wall.isVertical) dx = - dx; else dy = - dy; }}
Code sketch for BallThread class:
public class BallThread extends Thread {
BallItem twin;
public void run() {
while (true) {
twin.draw(); /*erase*/ twin.move(); twin.draw();
}
}
}
Implementation of Twin pattern
Following issues should be considered:
- Data abstraction - partner classes of the twin class have to be tightly coupled, as probably they have to access each other private fields and methods. In Java, this can be achieved by placin partner classes in to a common packeage and providing package visibility for the required fields and methods. In Module-3 and Oberon, partner classes can be place in a common module
- Efficiency - Due to the reason that Twin pattern uses composition whice required forward messaging, Twin pattern may be less efficient than inheritance. And, Multiple inheritance slighty less efficient than single intheritance, so, the overhead will not be a major problem.[1][2]
See also
- Adapter Pattern, specially Two-Way-Adapter
References
- ^ a b c d e f g h i j k l m Mössenböck, H., Twin - A Design Pattern for Modelling Multiple Inheritance, University of Linz, Institute of Practice Computer Science
- ^ Stroustrup, B. (May 1989), Multiple Inheritance for C++, Helsinki: = Proceeding EUUG Spring Conference
{{citation}}
: CS1 maint: extra punctuation (link) CS1 maint: year (link)