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

Should component know its host?

There is a component class inside the host class:

class Host
{
Component *c;
....

In some cases Component needs to call the host and
I guess the only way is that you pass the host for it:

Component::Component(Host *h)
{
h->Do_Something();
....

Somehow I think this is bad design, but how to avoid it?
Aug 14 '08 #1
5 1582
Krice wrote:
There is a component class inside the host class:

class Host
{
Component *c;
...

In some cases Component needs to call the host and
I guess the only way is that you pass the host for it:

Component::Component(Host *h)
{
h->Do_Something();
...

Somehow I think this is bad design, but how to avoid it?
You could extract the behaviour the component needs into a separate
class and derive your host from that class. That way there is no direct
dependency.

Whether the existence of the dependency is acceptable depends on your
model, I guess (pun intended). Parents and children depend on each
other for different functionality, so, bidirectional interaction is not
unheard of in the real world...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 14 '08 #2
On Aug 14, 4:20*pm, Krice <pau...@mbnet.fiwrote:
There is a component class inside the host class:

class Host
{
* Component *c;
...

In some cases Component needs to call the host and
I guess the only way is that you pass the host for it:

Component::Component(Host *h)
{
* h->Do_Something();
...

Somehow I think this is bad design, but how to avoid it?
You could use a generic callback mechanism. Check out
std::tr1::function if you have TR1 available or Boost.Function if you
don't. It would make your code look something like:

Component::Component( const std::tr1::function& prologFn )
{
prologFn();
// ...
}

Also similar is _Modern C++ Design_'s Loki::Function (http://loki-
lib.sourceforge.net).

Cheers! --M
Aug 14 '08 #3
Krice <pa****@mbnet.fiwrote:
There is a component class inside the host class:

class Host
{
Component *c;
...

In some cases Component needs to call the host and
I guess the only way is that you pass the host for it:

Component::Component(Host *h)
{
h->Do_Something();
...

Somehow I think this is bad design, but how to avoid it?
The typical OO solution is to make an abstract base class:

class HostBase {
public:
virtual void Do_Something() = 0;
};

class Base : public HostBase {
public:
void Do_Something();
};

class Component {
public:
Component( HostBase* hb ) {
hb->Do_Something();
}
};

I'm not so sure, though, if this is the best solution in this case... If
the component really is "inside" the host, then maybe the host should
call Do_Something() itself before constructing the component. If the
component is being created outside the host and passed into it, then
maybe the creator of the component should be calling Do_Something() on
the host.
Aug 15 '08 #4
Krice <pa****@mbnet.fikirjutas:
There is a component class inside the host class:

class Host
{
Component *c;
...

In some cases Component needs to call the host and
I guess the only way is that you pass the host for it:

Component::Component(Host *h)
{
h->Do_Something();
...

Somehow I think this is bad design, but how to avoid it?
In the component constructor it is quite easy to avoid, as suggested by
Daniel - just use a factory function for creating the components, which
additionally calls Host::Do_Something() before or after the construction
as appropriate.

The situation gets more complicated if the component needs to call host
also later, from other member functions. By some reason (probably bad
design) I have had many such cases. Just passing the host pointer as you
show above, this is actually not so bad. What would be worse is to store
the host pointer or reference inside the component object. This could
potentially create a whole bunch of maintenance and lifetime issues. As a
simple solution, one could just pass the host pointer each time the
component needs it.

A side note: I would probably use a reference instead of a pointer here,
as most probably the Host object should remain alive throughout the call.

And yes, as other posters mentioned, it might be a good idea to restrict
the amount of interdependencies by passing a pointer or reference to a
narrower interface instead of the full host (interface = ABC, host
derived from the interface).

hth
Paavo
Aug 15 '08 #5
On 14 elo, 23:29, Victor Bazarov <v.Abaza...@comAcast.netwrote:
You could extract the behaviour the component needs into a separate
class and derive your host from that class.
I want to avoid deriving.. anyway, the solution was simple
after all. I wanted to control the host from the component,
but I guess it should be the other way around so now the host
is controlling the (now simplified) component.
Aug 15 '08 #6

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

Similar topics

5
by: Al Davis | last post by:
Note: I tried cross-posting this message to several newsgoups, including comp.lang.perl.misc, c.l.p.moderated, comp.infosystems.www.authoring.cgi, comp.lang.javascript and comp.lang.php. Nothing...
1
by: TJ | last post by:
Anyone know how to display process id's (normally found by going to Component Services and switching to applications to 'Status View') in a web page? I'm trying to keep remote users from having...
1
by: M. D'Costa | last post by:
Hello, There are a number of Timer classes within the .NET framework. Am not very clear as to which component can be used, there is System.Timers.Timer, System.Windows.Forms.Timer,...
1
by: Dotnetjunky | last post by:
Hi, I'm looking for a Component which can help me manipulate (read/write/parse) and/or display SVG file format. Anybody here knows a robust component which can do that ? (commercial product...
2
by: Berata | last post by:
Hello all, in VB6 we were able to create ActiveX-DLL's (In Process Components) and ActiveX-Exe's (Out of Process Components). We habe build up an application that exists of an main module...
3
by: ytrewq | last post by:
Should dynamic ("expando") properties be restricted to native and user-defined objects? Or should host objects - such as references to the browser or a plug-in or to the document and its elements -...
1
by: Rafael Veronezi | last post by:
Hello, I need to write a .net component, wich will output data in html. Like, I call a method, and that method must write in the current stream that will be sent to the client, it's pure html that...
0
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
Hi, Is it possible to access an htc file from the windows webbrowser control? I am receiving the following error: "An error has occurred in the script on this page." "Error: Access is denied...
6
by: Laphan | last post by:
Hi All I can't get round this because it's a local government thing, but I've done a CMS for a school (in ASP classic) that allows them to upload gifs, jpgs, pdfs and docs to their site for the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.