Delegation pattern
Encyclopedia
In software engineering
, the delegation pattern is a design pattern
in object-oriented programming
where an object
, instead of performing one of its stated tasks, delegates that task to an associated helper object. There is an Inversion of Responsibility in which a helper object, known as a delegate, is given the responsibility to execute a task for the delegator. The delegation pattern is one of the fundamental abstraction
patterns that underlie other software patterns such as composition
(also referred to as aggregation), mixin
s and aspect
s.
example, the class
has a method. This print method, rather than performing the print itself, delegates to class . To the outside world it appears that the class is doing the print, but class is the one actually doing the work.
Delegation is simply passing a duty off to someone/something else. Here is a simple example:
, delegation can be made more flexible and typesafe. "Flexibility" here means that need not refer to or in any way, as the switching of delegation is abstracted from . Needless to say, and don't count as references to and . In this example, class can delegate to either class or class . Class has methods to switch between classes and . Including the clauses improves type safety
, because each class must implement the methods in the interface. The main tradeoff is more code.
version of the complex Java example above. Since C++ does not have an interface construct, a pure virtual class plays the same role. The advantages and disadvantages are largely the same as in the Java example.
The output:
version of the complex Java example above.
framework. The following example is an Objective-C version of the complex Java example above.
See also
External links
Software engineering
Software Engineering is the application of a systematic, disciplined, quantifiable approach to the development, operation, and maintenance of software, and the study of these approaches; that is, the application of engineering to software...
, the delegation pattern is a design pattern
Design pattern
A design pattern in architecture and computer science is a formal way of documenting a solution to a design problem in a particular field of expertise. The idea was introduced by the architect Christopher Alexander in the field of architecture and has been adapted for various other disciplines,...
in object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
where an object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...
, instead of performing one of its stated tasks, delegates that task to an associated helper object. There is an Inversion of Responsibility in which a helper object, known as a delegate, is given the responsibility to execute a task for the delegator. The delegation pattern is one of the fundamental abstraction
Abstraction (computer science)
In computer science, abstraction is the process by which data and programs are defined with a representation similar to its pictorial meaning as rooted in the more complex realm of human life and language with their higher need of summarization and categorization , while hiding away the...
patterns that underlie other software patterns such as composition
Object composition
In computer science, object composition is a way to combine simple objects or data types into more complex ones...
(also referred to as aggregation), mixin
Mixin
In object-oriented programming languages, a mixin is a class that provides a certain functionality to be inherited or just reused by a subclass, while not meant for instantiation , Mixins are synonymous functionally with abstract base classes...
s and aspect
Aspect (computer science)
In computer science, an aspect of a program is a feature linked to many other parts of the program, but which is not related to the program's primary function. An aspect crosscuts the program's core concerns, therefore violating its separation of concerns that tries to encapsulate unrelated functions...
s.
Simple
In this JavaJava (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
example, the class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
has a method. This print method, rather than performing the print itself, delegates to class . To the outside world it appears that the class is doing the print, but class is the one actually doing the work.
Delegation is simply passing a duty off to someone/something else. Here is a simple example:
Complex
By using interfacesInterface (Java)
An interface in the Java programming language is an abstract type that is used to specify an interface that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations...
, delegation can be made more flexible and typesafe. "Flexibility" here means that need not refer to or in any way, as the switching of delegation is abstracted from . Needless to say, and don't count as references to and . In this example, class can delegate to either class or class . Class has methods to switch between classes and . Including the clauses improves type safety
Type safety
In computer science, type safety is the extent to which a programming language discourages or prevents type errors. A type error is erroneous or undesirable program behaviour caused by a discrepancy between differing data types...
, because each class must implement the methods in the interface. The main tradeoff is more code.
C# example
This is a C# example of the complex Java example above.C++ example (complex)
This example is a C++C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
version of the complex Java example above. Since C++ does not have an interface construct, a pure virtual class plays the same role. The advantages and disadvantages are largely the same as in the Java example.
The output:
A: doing f
A: doing g
A: cleaning up.
B: doing f
B: doing g
B: cleaning up.
Eiffel example (complex)
This example is a EiffelEiffel
-Engineering:* Eiffel Tower in Paris, designed by Gustave Eiffel in motif as twin to the Eifel ridge, re-dubbed Maria Pia Bridge, previously built in o Porto, Portugal - The Twin Towers of Eiffel...
version of the complex Java example above.
Objective-C example
Delegation is very common in the CocoaCocoa (API)
Cocoa is Apple's native object-oriented application programming interface for the Mac OS X operating system and—along with the Cocoa Touch extension for gesture recognition and animation—for applications for the iOS operating system, used on Apple devices such as the iPhone, the iPod Touch, and...
framework. The following example is an Objective-C version of the complex Java example above.
Perl
Python Example (complex)
Ruby on Rails example (complex)
Tcl
PHP
See also
- Delegation (programming)Delegation (programming)In object-oriented programming, there are two related notions of delegation.* Most commonly, it refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement...
- Design patternDesign pattern (computer science)In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...
- Post-object programming
External links
Delegation (programming)
In object-oriented programming, there are two related notions of delegation.* Most commonly, it refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement...
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...
- Delegation on Rosetta CodeRosetta CodeRosetta Code is a wiki-based programming chrestomathy website with solutions to various programming problems in many different programming languages. It was created in 2007 by Mike Mol. Rosetta Code includes 450 programming tasks, and covers 351 programming languages...