473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

delegation

Hello!

What does it mean with delegation and can you give me one example.

//Tony
Jul 23 '05 #1
3 2396
Tony Johansson wrote:
What does it mean with delegation and can you give me one example.

class Calculator {
public:
int calculate(int,int);
};

class NeedsSomeCalculation {
Calculator calc;
public:
explicit NeedsSomeCalculation(Calculator const& c) : calc(c) {}
void do_something(int a, int b) {
int result = calc.calculate(a,b); // delegation
}
};

Calculator global_calculator;

int main() {
NeedsSomeCalculation obj(global_calculator);
obj.do_something(1,2);
}

In the example above, instead of performing some calculation in the
object itself, the NeedSomeCalculation instance is made to rely on
another object (of another class) to do some work for it. That's
delegation.

Often delegation is used when something is implemented in terms of
something else:

class PhoneNumber : private std::string { // notice 'private'
public:
static bool isvalidchar(char);

PhoneNumber(const char*); // will verify the format and
// throw if invalid
void append(char c) {
if (isvalidchar(c))
std::string::append(1, c); // delegation!
}

size_t length() const {
return std::string::length(); // delegation!
}
};

'PhoneNumber' is implemented in terms of 'std::string', but cannot
be used anywhere where 'std::string' can. Using OO definitions we
say that 'PhoneNumber' and 'std::string' _do_not_ have the "is-a"
relationship.

V
Jul 23 '05 #2
Tony Johansson wrote:
What does it mean with delegation and can you give me one example.


Delegation is often an alternative to inheritance.

Suppose you want a special set class that basically behaves like std::set,
and provides the usual functions insert(element), find(), end(). But you
want that insert(element) inserts a *modified* version of the element, e.g.
all-uppercase if the elements are string.

You have two possibilities: inheritance or delegation.

Inheritance:
Make a subclass my_set of std::set and override the insert(element) function
so that it inserts a modified version of the element. (Maybe that's not
even possible if std::set::insert is not virtual).

class my_set : public std::set {
// ...
};
Delegation would be the alternative:
Create you own class my_set, with the member functions insert(element),
find(), end(). It also has a member variable std::set theSet. For the
find() and end() function, you just delegate the calls to theSet! For the
insert(element) function, you modify the element and call
theSet.insert(modifiedElement):

Basically, it looks like this:

class my_set {
public:
// delegate call:
std::set<T>::iterator end(){
return theSet.end();
}
// ... other delegate functions: insert, find
private:
std::set theSet; // delegate all calls to this set object!
};
Delegation is often better than inheritance, for several reasons: You
provide only the functions that are needed, here: insert, find, end. If you
*inherit* you inherit a bunch of other functions whose behavior you don't
know and can't control. std::set might contain other insert functions that
you haven't thought of. If you inherit from std::set your class would have
all these functions, and they do *not* insert a modified version of the
element, as you intended.

Even worse, std::set might change its API (although unlikely for STL
classes, but certainly likely for other classes that you might want to
subclass), adding some different insert() functions, and your subclass
would suddenly be broken! Because those new insert functions would not
insert *modified* versions of the element, which was the point of your
my_set class.

There are more reasons why delegation is better than inheritance, but I
can't remember off the top of my head.

Jul 23 '05 #3
Markus Dehmann wrote:
Tony Johansson wrote:

What does it mean with delegation and can you give me one example.

Delegation is often an alternative to inheritance.

Suppose you want a special set class that basically behaves like std::set,
and provides the usual functions insert(element), find(), end(). But you
want that insert(element) inserts a *modified* version of the element, e.g.
all-uppercase if the elements are string.

You have two possibilities: inheritance or delegation.

Inheritance:
Make a subclass my_set of std::set and override the insert(element) function
so that it inserts a modified version of the element. (Maybe that's not
even possible if std::set::insert is not virtual).

class my_set : public std::set {
// ...
};
Delegation would be the alternative:
Create you own class my_set, with the member functions insert(element),
find(), end(). It also has a member variable std::set theSet. For the
find() and end() function, you just delegate the calls to theSet! For the
insert(element) function, you modify the element and call
theSet.insert(modifiedElement):

Basically, it looks like this:

class my_set {
public:
// delegate call:
std::set<T>::iterator end(){
return theSet.end();
}
// ... other delegate functions: insert, find
private:
std::set theSet; // delegate all calls to this set object!
};
Delegation is often better than inheritance, for several reasons: You
provide only the functions that are needed, here: insert, find, end. If you
*inherit* you inherit a bunch of other functions whose behavior you don't
know and can't control. std::set might contain other insert functions that
you haven't thought of. If you inherit from std::set your class would have
all these functions, and they do *not* insert a modified version of the
element, as you intended.

Even worse, std::set might change its API (although unlikely for STL
classes, but certainly likely for other classes that you might want to
subclass), adding some different insert() functions, and your subclass
would suddenly be broken! Because those new insert functions would not
insert *modified* versions of the element, which was the point of your
my_set class.

There are more reasons why delegation is better than inheritance, but I
can't remember off the top of my head.


All good points...

one other is that we can have dynamic relationships between the class
and its delegate(s) - i.e. we can change who the delegate is at runtime.
Whereas inheritance is a static relationship.

The delegate can also be of a concrete type that we don't know or didn't
know at compile time - so long as all delegates support the same interface.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rene Pijlman | last post by:
Section 6.5 "What is delegation?" of the FAQ says: "Python programmers can easily implement delegation. For example, the following class implements a class that behaves like a file but converts...
3
by: Jacob | last post by:
Hello All, I am trying to serve out some content via IIS that is hosted on a remote fileserver, and am unable to get the delegation working correctly. Our setup is as follows: Local LAN...
6
by: DPfan | last post by:
Is the following so-called "delegation"? If not how to make some changes so that the F class delegates its operation to an E instance. On the other hand the following code runs without any...
2
by: | last post by:
Hi, I am working on a n-tier app using remoting. I am using the VS 2005 beta 2. My server needs to access a remote resources on behalf on the connected user. I have configured my server like :...
2
by: russell.lane | last post by:
I'm building out a pretty standard n-tier web application. The stack includes application/presentation, biz logic, and data access layers on top of an SQL server back end. We want to use...
4
by: JimLad | last post by:
In advance, sorry if this is the wrong group... SQL Server 2000 SP3 on Server 2003. SQL Account and Computer both Trusted for Delegation. Given SPN. IIS 5.0 on W2000. Kerberos enabled....
6
by: Marc Castrechini | last post by:
This is a classic double hop delegation issue, however its the first time we are setting this up so we are doing something incorrectly. If we run through the IDE or using a localhost path on the...
3
by: Patrick | last post by:
Hello I have the following scenario - SQL 2005 server (serversql) - Windows 2003 with IIS (serveriis) - Windows 2003 ADS (serverads) I want to connect to an intranet application using NTML...
5
by: =?Utf-8?B?TWF5ZXI=?= | last post by:
Hi, I'm using two form classes and I would like all methods of the second class (the child class) to be managed by the first class (the main class). Is delegation the best solution for me? If so,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.