ArticleslgStudy

computer science

Delegation pattern

Delegation 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 Delegation pattern rather than just read about it. In short: In software engineering, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as inheritance. In delegation, an object handles a request by delegating to a second object (the delegate).

Key takeaways

  • Delegation 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 Delegation pattern to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Delegation pattern from memory before moving on to harder problems.

Reference excerpt

In software engineering, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as inheritance. In delegation, an object handles a request by delegating to a second object (the delegate). The delegate is a helper object, but with the original context. With language-level support for delegation, this is done implicitly by having self in the delegate refer to the original (sending) object, not the delegate (receiving object). In the delegate pattern, this is instead accomplished by explicitly passing the original object to the delegate, as an argument to a method. "Delegation" is often used loosely to refer to the distinct concept of forwarding, where the sending object simply uses the corresponding member on the receiving object, evaluated in the context of the receiving object, not the original object. This article uses "sending object/receiving object" for the two objects, rather than "receiving object/delegate", emphasizing which objects send and receive the delegation call, not the original call.

Definition In the Introduction to Gamma et al. 1994, delegation is defined as:

Delegation is a way to make composition as powerful for reuse as inheritance [Lie86, JZ91]. In delegation, two objects are involved in handling a request: a receiving object delegates operations to its delegate. This is analogous to subclasses deferring requests to parent classes. But with inheritance, an inherited operation can always refer to the receiving object through the this member variable in C++ and self in Smalltalk. To achieve the same effect with delegation, the receiver passes itself to the delegate to let the delegated operation refer to the receiver.

Example In the example below (using the Kotlin programming language), the class Window delegates the area() call to its internal Rectangle object (its delegate).

Language support Some languages have special support for delegation built in. For example, in the Kotlin programming language the by keyword delegates to another object's interface:

A more practical example is the following:

In this example, LoggingRepository implements the same interface as Repository, while wrapping another Repository instance. The save method is specialized to add logging, whereas the remaining methods are forwarded automatically to the delegate object. This avoids boilerplate and makes it possible to extend the behavior of a Repository without being tied to a particular concrete implementation such as FileRepository. By contrast, an inheritance-based solution would require subclassing a specific implementation:

This approach couples LoggingRepository to FileRepository, whereas interface delegation allows the same wrapper to be used with any `Repository` implementation. In Kotlin, interface delegation using by is implemented by the compiler through generated forwarding methods. For example, the previous Window class is equivalent to code such as:

When interface delegation is combined with manual overriding, only the members not explicitly overridden are forwarded automatically. For example:

This program will print

The result shows that greet is handled by the explicit override in LoudGreeter, whereas greetTwice is forwarded to DefaultGreeter. Inside DefaultGreeter.greetTwice, the receiver remains the delegate object itself, so the internal calls to greet() resolve to DefaultGreeter.greet() rather than LoudGreeter.greet(). For this reason, Kotlin's by feature is often described as delegation, but operationally it is closer to compiler-generated forwarding than to stronger forms of delegation in which the original receiver context is preserved.

See also Delegation (object-oriented programming) Forwarding (object-oriented programming) Aspect-oriented programming Delegation (computing) Design pattern Facade pattern Schizophrenia (object-oriented programming)

References

External links What Is Delegation, WikiWikiWeb Delegation on Rosetta Code

Worked examples

Example 1 — a first encounter with Delegation pattern

Start with the simplest possible case. Write down what Delegation 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 Delegation 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 Delegation 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 Delegation pattern

In research
Delegation 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 Delegation 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
Delegation 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 Delegation 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 “Delegation pattern” →

How to study Delegation pattern in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Delegation 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 Delegation pattern out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Delegation pattern in simple terms?

In software engineering, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as inheritance. In delegation, an object handles a request by delegating to a second object (the delegate).

Why does Delegation 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 Delegation 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 Delegation pattern.

Tags

  • Software design patterns

Keep exploring