473,322 Members | 1,703 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,322 software developers and data experts.

Delegation

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 problem. Is there any
potential problems with it?

class E
{
public:
void Draw_E(int a, int b) { cout << "Draw in E " << a*b<< endl; }
};

class F
{
E *e;
public:
void Draw_F() { e->Draw_E( 123, 456 ); cout << "Draw in F" << endl; }
};

int main(void){
F f;
f.Draw_F();
return 0;
}
Jul 19 '05 #1
6 4407
> Is the following so-called "delegation"?

Yep.
If not how to make some changes so
that the F class delegates its operation to an E instance.
Delegation has no direct language support, so you "just do it".

Delegation means that users of F are unaware that a secret E performed
Draw_F.
On the other hand the following code runs without any problem. Is there any potential problems with it?


Delegation is good if users of F generally remain unaware of all Es in the
program. This is an example of "decoupling".

Delegation is bad if F has no other reason to exist, or if users of F could
have used E and simplified the program. Maybe F should be unaware of E.

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces
Jul 19 '05 #2
Phlip <ph*******@yahoo.com> wrote in message
news:pj*****************@newssvr32.news.prodigy.co m...
Is the following so-called "delegation"?
Yep.
If not how to make some changes so
that the F class delegates its operation to an E instance.


Delegation has no direct language support, so you "just do it".

Delegation means that users of F are unaware that a secret E performed
Draw_F.
On the other hand the following code runs without any problem. Is there

any
potential problems with it?


Delegation is good if users of F generally remain unaware of all Es in the
program. This is an example of "decoupling".

Delegation is bad if F has no other reason to exist, or if users of F

could have used E and simplified the program. Maybe F should be unaware of E.

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces


Thank you very much!

Notice that in class F, there's no instance of class E, but a pointer. Then
inside Draw_F() function Draw_E() is called with a statement "e->Draw_E."
Is this good way to do delegation? Is this good coding in C++ in general?
Jul 19 '05 #3
<DPfan>
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 problem. Is there any
potential problems with it?

class E
{
public:
void Draw_E(int a, int b) { cout << "Draw in E " << a*b<< endl; }
};

class F
{
E *e;
public:
void Draw_F() { e->Draw_E( 123, 456 ); cout << "Draw in F" << endl; }
};

int main(void){
F f;
f.Draw_F();
return 0;
}

</>
I wonder why it does run correct. Possibly luck. The
object pointed to by E*e is never created. I added
a constructor and a destructor.

-X

#include<iostream>
class E
{
public:
void Draw_E(int a, int b) { cout << "Draw in E " << a*b<< endl; }
};

class F
{
E *e;

public:
F():e(new E){}
virtual~F(){delete e;}
void Draw_F() { e->Draw_E( 123, 456 ); cout << "Draw in F" << endl; }

};

int main(void){
F f;
f.Draw_F();
return 0;
}


Jul 19 '05 #4
> > Delegation is good if users of F generally remain unaware of all Es in
the
program. This is an example of "decoupling".

Delegation is bad if F has no other reason to exist, or if users of F could
have used E and simplified the program. Maybe F should be unaware of E.

Notice that in class F, there's no instance of class E, but a pointer. Then inside Draw_F() function Draw_E() is called with a statement "e->Draw_E."
Is this good way to do delegation? Is this good coding in C++ in general?


No. Always use the simplest and weakest thing.

Agent Mulder pointed out you had no E instance, so he added 'new'.

You could also fix that by removing the *, and replacing the -> with a dot .

In terms of OO design theory, delegation does not require *, or . ; only
that F know an E ready for the job. "Know" means "can access".

In terms of program stability, don't point without overwhelming need. Prefer
actual things, and pass them by reference. Don't point to character arrays -
use std::string. Don't point to arrays - use std::list or std::vector.

So your simplest delegation lets F have a member of type E.

--
Phlip
Jul 19 '05 #5
> Notice that in class F, there's no instance of class E, but a pointer. Then
inside Draw_F() function Draw_E() is called with a statement "e->Draw_E."
Is this good way to do delegation? Is this good coding in C++ in general?


The main advantage of delegation is that you can reduce the dependencies.
In this example, you can get by without including the header file
for E within F if the implemntation of the delegating function is placed
in the CPP file.

See the following article for decoupling of header files:

http://www.eventhelix.com/RealtimeMa...dePatterns.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Cases in PDF
Jul 19 '05 #6
> The main advantage of delegation is that you can reduce the dependencies.
In this example, you can get by without including the header file
for E within F if the implemntation of the delegating function is placed
in the CPP file.
Per "always use the simplest and weakest thing", then if you have more than
one user of E, or if E's header requirements are more complex than F's, you
may benefit from putting E in a different header from F's, and
forward-declaring it in F's header.

However, if nobody else uses Es without Fs, you may lose the benefits of
putting it in a separate header.

Organically, as E grows the pressures increase to remove its dependencies
from F's. But as E shrinks pressures increase to put it inside F's header,
or inside F, or even inside F's implementation file.
See the following article for decoupling of header files:


/Large Scale C++ Software Design/ and /Exceptional C++/.

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces
Jul 19 '05 #7

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...
3
by: Tony Johansson | last post by:
Hello! What does it mean with delegation and can you give me one example. //Tony
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.