ArticleslgStudy

computer science

Twin pattern

Twin pattern is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Twin pattern rather than just read about it. In short: 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.

Twin pattern — main illustration
Twin pattern — illustration

Key takeaways

  • Twin pattern belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Twin pattern to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Twin pattern from memory before moving on to harder problems.

Reference excerpt

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.

Definition 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.

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:

Code sketch for GameItem class:

Code sketch for the BallItem class:

Code sketch for BallThread class:

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

Illustrations

Twin pattern: Twin pattern
Twin pattern

Worked examples

Example 1 — a first encounter with Twin pattern

Start with the simplest possible case. Write down what Twin pattern claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Twin pattern before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Twin pattern ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Twin pattern

In research
Twin pattern appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Twin pattern in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Twin pattern is common in secondary-school and first-year university syllabi. It links to neighbouring topics Software design patterns, so understanding it makes those chapters shorter.
In everyday life
Look for Twin pattern outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Twin pattern” →

How to study Twin pattern in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Twin pattern means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Twin pattern out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Twin pattern in simple terms?

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.

Why does Twin pattern matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Twin pattern?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Twin pattern.

Tags

  • Software design patterns

Keep exploring