473,587 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::Comp onent(Host *h)
{
h->Do_Something() ;
....

Somehow I think this is bad design, but how to avoid it?
Aug 14 '08 #1
5 1597
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::Comp onent(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.f iwrote:
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::Comp onent(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::funct ion if you have TR1 available or Boost.Function if you
don't. It would make your code look something like:

Component::Comp onent( const std::tr1::funct ion& 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.f iwrote:
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::Comp onent(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.f ikirjutas:
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::Comp onent(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_Someth ing() 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 interdependenci es 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...@com Acast.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
3463
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 appeared on my news server, so I'm trying again - this time posting a separate copy of the message to each group. I'm thinking this should be...
1
3256
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 to terminal server or use com+ to view the process id's they need for asp debugging (via attaching to a process (remote debugging via dcom and...
1
272
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, System.Threading.Timer for example.....any help will be appreciated as to which component is better for use in some cases, more than others. Thank you, Marise
1
2079
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 is fine too.) I've searched Google and found several ones, but up to now, none of them convinces me. Thanks a lot
2
5308
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 (Standard Exe) that calls and controls other modules (ActiveX-Exe). We decided for ActiveX-Exe because they run in different processes. That means if...
3
3020
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 - also allow them? Adding (and removing) object properties dynamically is an acceptable and common practice in JavaScript, and greatly adds to the...
1
987
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 will be written... I would like to know what's the best method to do that... It's a component, because it'll be use on a lot of projects. I think...
0
1831
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 to " "Url: about:blank" I am able to access the htc file when i run the host web page as a normal
6
4933
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 usual CMS stuff. Works fine on my host as I use ASPupload and I have one FTP location for my data, but I've now found out that I've got to put the...
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6619
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.