encyclopegia.org

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.

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. ==Applicability== The twin pattern can be used: to model multiple inheritance in languages that don't support it. to avoid some problems of multiple inheritance. ==Structure== 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: ==Collaborations== 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. Clients that rely on the protocols of parent classes communicate with objects of the respective child class. ==Sample code== 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 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 the Twin pattern== The 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 placing the partner classes into a common package and providing package visibility for the required fields and methods. In Modula-3 and in Oberon, partner classes can be placed in a common module. Efficiency - Since the Twin pattern uses composition which requires message forwarding, the Twin pattern may be less efficient than inheritance. However, since multiple inheritance is slightly less efficient than single inheritance anyway, the overhead will not be a major problem. Cyclic reference - The Twin pattern relies on each twin referencing the other twin, which causes a cyclic reference scenario. Some languages may require such cyclic references to be handled specially to avoid a memory leak. For example, one reference may need to be made 'weak' to allow the cycle to break. ==See also== Adapter Pattern, specially Two-Way-Adapter ==References==

References

  1. Stroustrup, B. (May 1989), Multiple Inheritance for C++ , Helsinki: Proceeding EUUG Spring Conference Wikipedia revision 1267008783, retrieved 2026-11-19 · retrieved 2026-11-18 · rev 1267008783
    Cite this reference

    MLA "Stroustrup, B. (May 1989), Multiple Inheritance for C++ , Helsinki: Proceeding EUUG Spring Conference Wikipedia revision 1267008783, retrieved 2026-11-19." Stroustrup, B. 2026-11-18. https://en.wikipedia.org/w/index.php?oldid=1267008783. Accessed 24 November 2026.

    Chicago "Stroustrup, B. (May 1989), Multiple Inheritance for C++ , Helsinki: Proceeding EUUG Spring Conference Wikipedia revision 1267008783, retrieved 2026-11-19." Stroustrup, B. 2026-11-18. https://en.wikipedia.org/w/index.php?oldid=1267008783.

    APA Stroustrup, B. (2026). Stroustrup, B. (May 1989), Multiple Inheritance for C++ , Helsinki: Proceeding EUUG Spring Conference Wikipedia revision 1267008783, retrieved 2026-11-19. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1267008783

  2. a b c d e f g h i j k l m n Mössenböck, H., Twin - A Design Pattern for Modelling Multiple Inheritance , University of Linz, Institute for System Software Wikipedia revision 1267008783, retrieved 2026-11-19 · retrieved 2026-11-18 · rev 1267008783
    Cite this reference

    MLA "a b c d e f g h i j k l m n Mössenböck, H., Twin - A Design Pattern for Modelling Multiple Inheritance , University of Linz, Institute for System Software Wikipedia revision 1267008783, retrieved 2026-11-19." a b c d e f g h i j k l m n Mössenböck, H. 2026-11-18. https://en.wikipedia.org/w/index.php?oldid=1267008783. Accessed 24 November 2026.

    Chicago "a b c d e f g h i j k l m n Mössenböck, H., Twin - A Design Pattern for Modelling Multiple Inheritance , University of Linz, Institute for System Software Wikipedia revision 1267008783, retrieved 2026-11-19." a b c d e f g h i j k l m n Mössenböck, H. 2026-11-18. https://en.wikipedia.org/w/index.php?oldid=1267008783.

    APA a b c d e f g h i j k l m n Mössenböck, H. (2026). a b c d e f g h i j k l m n Mössenböck, H., Twin - A Design Pattern for Modelling Multiple Inheritance , University of Linz, Institute for System Software Wikipedia revision 1267008783, retrieved 2026-11-19. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1267008783

Cite this article

MLA

"Twin pattern." encyclopegia.org, 2025, https://encyclopegia.org/wiki/twin-pattern. Accessed 24 November 2026.

Chicago

"Twin pattern." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/twin-pattern.

APA

encyclopegia.org. (2025). Twin pattern. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/twin-pattern

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1267008783 · 0.5 · snapshot enwiki-001267008783

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.