Twin pattern
Find sources: "Twin pattern" – news · newspapers · books · scholar · JSTOR (April 2020) (Learn how and when to remove this message)
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]
Definition
[edit ]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]
Applicability
[edit ]The twin pattern can be used:
- to model multiple inheritance in languages that don't support it.
- to avoid some problems of multiple inheritance.[1]
Structure
[edit ]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:
Collaborations
[edit ]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]
Sample code
[edit ]The following code is a sketched implementation of a computer game board with moving balls.
Class for the game board:
publicclass GameboardextendsCanvas{ publicintwidth,height; publicGameItemfirstItem; ... }
Code sketch for GameItem class:
publicabstractclass GameItem{ Gameboardboard; intposX,posY; GameItemnext; publicabstractvoiddraw(); publicabstractvoidclick(MouseEvente); publicabstractbooleanintersects(GameItemother); publicabstractvoidcollideWith(GameItemother); publicvoidcheck(){ GameItemx; for(x=board.firstItem;x!=null;x=x.next) if(intersects(x)) collideWith(x); } publicstaticBallItemnewBall(intposX,intposY,intradius){//method of GameBoard BallItemballItem=newBallItem(posX,posY,radius); BallThreadballThread=newBallThread(); ballItem.twin=ballThread; ballThread.twin=ballItem; returnballItem; } }
Code sketch for the BallItem class:
publicclass BallItemextendsGameItem{ BallThreadtwin; intradius;intdx,dy; booleansuspended; publicvoiddraw(){ board.getGraphics().drawOval(posX-radius,posY-radius,2*radius,2*radius); } publicvoidmove(){posX+=dx;posY+=dy;} publicvoidclick(){ if(suspended) twin.resume(); else twin.suspend(); suspended=!suspended; } publicbooleanintersects(GameItemother){ if(otherinstanceofWall) returnposX-radius<=other.posX &&other.posX<=posX+radius ||posY-radius<=other.posY &&other.posY<=posY+radius; else returnfalse; } publicvoidcollideWith(GameItemother){ Wallwall=(Wall)other; if(wall.isVertical) dx=-dx; else dy=-dy; } }
Code sketch for BallThread class:
publicclass BallThreadextendsThread{ BallItemtwin; publicvoidrun(){ while(true){ twin.draw(); /*erase*/ twin.move(); twin.draw(); } } }
Implementation of the Twin pattern
[edit ]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.[1] [2]
- 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
[edit ]- Adapter Pattern, specially Two-Way-Adapter