Loading AI tools
Software design pattern From Wikipedia, the free encyclopedia
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.[1]
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.[1]
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. [1]
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. [1]
Clients 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 respective child class.[1]
The 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();
}
}
}
The following issues should be considered:
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.