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

Template Method in multi leveled inheritence

Hello All I hope you could help me.

Let's examine the next inheritance tree:

class A
{
public: void init()
{ // Do things
initialiseClass();
}
protected:
virtual void initialiseClass() = 0;
};

class B : public A
{
protected:
virtual void intialiseClass()
{
//Do more things
}
};

What to do with class C that inherits from B and adds more
initialization data?
I can call Super but than Template Method loses his meaning...
Is there a solution? (except composition)
Sep 23 '08 #1
7 1185
ManicQin wrote:
Hello All I hope you could help me.

Let's examine the next inheritance tree:

class A
{
public: void init()
{ // Do things
initialiseClass();
}
protected:
virtual void initialiseClass() = 0;
};

class B : public A
{
protected:
virtual void intialiseClass()
{
//Do more things
}
};

What to do with class C that inherits from B and adds more
initialization data?
What would be the problem?
I can call Super but than Template Method loses his meaning...
Uh... Please elaborate.
Is there a solution? (except composition)
If the derived class needs to call the base class' member function to
let it do the base class part of the job, then you have no other way
than to call the "super" (I am guessing you mean this:

void C::initialiseClass() {
B::initialiseClass(); // calling the "Super"
// Do even more things
}

). I guess you can see that either I don't understand the problem or
there isn't any (and you're imagining it).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 23 '08 #2
On Sep 23, 6:56*am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
ManicQin wrote:
Hello All I hope you could help me.
Let's examine the next inheritance tree:
class A
{
public: void init()
{ // Do things
* initialiseClass();
}
protected:
virtual void initialiseClass() = 0;
};
class B : public A
{
protected:
virtual void intialiseClass()
{
*//Do more things
}
};
What to do with class C that inherits from B and adds more
initialization data?

What would be the problem?
I can call Super but than Template Method loses his meaning...

Uh... *Please elaborate.
I think that OP is talking about the Template design pattern from GoF.
I think he means directly call parent's initializeClass member
function.

Other than that, I agree with your solution.

Sep 23 '08 #3
Victor Bazarov wrote:
ManicQin wrote:
>Hello All I hope you could help me.

Let's examine the next inheritance tree:

class A
{
public: void init()
{ // Do things
initialiseClass();
}
protected:
virtual void initialiseClass() = 0;
};

class B : public A
{
protected:
virtual void intialiseClass()
{
//Do more things
}
};

What to do with class C that inherits from B and adds more
initialization data?

What would be the problem?
That, with the template method pattern, you don't call your
base class' virtual functions. Instead your base class calls
your virtual function.
>I can call Super but than Template Method loses his meaning...

Uh... Please elaborate.
Read up on the pattern.
>Is there a solution? (except composition)
[...]
V
Schobi
(who is also interested in this but doesn't know a good solution either)
Sep 24 '08 #4
Hendrik Schober wrote:
[..]
>>I can call Super but than Template Method loses his meaning...

Uh... Please elaborate.

Read up on the pattern.
Hendrik, that is not a good answer. If you want a discussion (and are
interested "in this") as you have alleged, you should not try to
antagonize those that participate in it. If you can elaborate on the
pattern's losing "his meaning", do. If not, well, don't.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 24 '08 #5
Victor Bazarov wrote:
Hendrik Schober wrote:
>[..]
>>>I can call Super but than Template Method loses his meaning...
Uh... Please elaborate.
Read up on the pattern.

Hendrik, that is not a good answer. If you want a discussion (and are
interested "in this") as you have alleged, you should not try to
antagonize those that participate in it.
I'm sorry if I did, it surely wasn't my intention.
If you can elaborate on the
pattern's losing "his meaning", do. If not, well, don't.
I'm not sure what you expect. The point of the pattern is
that the base class calls the derived class, freeing the
derived class' implementer of worrying whether to call the
base class' implementation of some virtual function before,
after, or amidst the derived class' algorithm. But in the
situation the OP describes, the implementer of C is back
at having to worry. I found the OP having explained the
situation clearly and don't see anything I could add.

Since I had the feeling you had missed the point of the
pattern, I suggested you read up on it. I'm sorry if that
offended you, but I honestly don't see what else (besides
ignoring you) I could have done.
V
Schobi
Sep 24 '08 #6
Hendrik Schober wrote:
Victor Bazarov wrote:
> If you can elaborate on the
pattern's losing "his meaning", do. If not, well, don't.

I'm not sure what you expect. The point of the pattern is
that the base class calls the derived class, freeing the
derived class' implementer of worrying whether to call the
base class' implementation of some virtual function before,
after, or amidst the derived class' algorithm. But in the
situation the OP describes, the implementer of C is back
at having to worry. I found the OP having explained the
situation clearly and don't see anything I could add.
[..]
Well, that's too bad. The pattern is not about freeing the implementer
from worrying whether to call the base class' implementation for
whatever the implementation may require, or not. It's about providing
the algorithm (sequence) and letting the derived class supply only
pieces, according to the requirements of the algorithm.

In the case of the OP, if the entire algorithm consists of the only call
to the 'initialise' function, then, in the hierarchy

class A {
public:
void init() { /// algorithm
initialise(); /// piece provided by the [most-]derived class
}
protected:
virtual void initialise() = 0;
};

class B : public A {
protected:
virtual void initialise() { /* whatever */ }
};

class C : public B {
protected:
virtual void initialise() { B::initialise(); /* whatever else */ }
};

the act of calling B::initialise() as the first step in C::initialise is
an implementation detail and AFAIUI does *not* violate any rules or the
intentions of the pattern.

If class 'C' is intended to be the "piece supplier" to 'A's algorithm,
the inheritance of 'C' from 'B' has nothing to do with it. If you look
at the pattern, its description involves two parts, the algorithm and
the components of the algorithm. Here they are provided by the same
class (which is perfectly fine), 'A'. 'B' derives from 'A' and provides
the piece[s] (the final overrider for 'initialise' member). If the user
wants to do something similar, they should derive from 'A', not from
'B'. That's how the pattern is actually defined.

'C' derives from 'B' - *why*? The OP did not say. For all I could
gather, it's something the users of 'B' can do, and the OP had no
control over it. So, the OP worries that because he didn't provide
enough protection, the user can do something wrong. So? The user will
always figure out a way to make a mistake or to misuse the classes they
are given. And we here cannot hold every poster's hand and keep
repeating, "Document your classes carefully, provide as much information
as possible to allow user to see the light, and stop worrying about the
user's screw-ups, you're not responsible..." What can be done
programmatically? Nothing, really, except denying the user the ability
to derive from 'B'...

There are two other newsgroups where the discussion on patterns is
better placed: comp.software.patterns and comp.object. I don't want to
appear unaccommodating, but if the OP or you have some doubts whether
allowing C::initialise to call B::initialise would somehow invalidate
the pattern, they should probably be cleared there, by experts in the field.

If there is a seeming collision between the rules of the language and
the pattern itself, it's probably true. But keep in mind that some
patterns (like the Template Method here) are somewhat simplistic and not
supposed to answer all questions or be the panacea for design problems
or protection against users' ignorance.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 24 '08 #7
On Sep 24, 11:23*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>
There are two other newsgroups where the discussion on patterns is
better placed: comp.software.patterns and comp.object. *I don't want to
appear unaccommodating, but if the OP or you have some doubts whether
allowing C::initialise to call B::initialise would somehow invalidate
the pattern, they should probably be cleared there, by experts in the field.
If Class C will just call C::Init there will be no "Violation"
No one will die and Life as we know it will be the same.
Giving different solutions for the same problem in one system is more
than OK
(as long as they do not collide) in this example they Dont collide.

But I was just wondering if there's a better solution.

Even though comp.software.patterns and comp.object are more related to
this
post I prefer to ask these sort of Q's in the comp.lang.c++ group...
I prefer the cplusplusian experienced answer more than the academic
one.

Thanks! ;)
Sep 25 '08 #8

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

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
4
by: Marc Schellens | last post by:
Just to make it sure: it is not possibel in C++ to have a template specialization for more than one type, right? ie. I have several template type parameters like string, int, long, char,...
3
by: Rennie deGraaf | last post by:
The attached code compiles and works properly when I comment out the declaration, definition, and invocations of the method 'eck'. With "eck" in there, g++ fails with ttest.cpp:23: template-id...
4
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? ...
2
by: Stephen Starkie | last post by:
Hi, For a while I have had some problem understanding just how template specialisation works in certain cases. In abridged form my code looks like this; --MyTemplate.h-- #ifndef MyTemplateH...
15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
28
by: Neo Geshel | last post by:
NOTE: PAST EXPERIENCE HAS SHOWN ME THAT MANY ON USENET FAIL TO READ ARTICLES PROPERLY PRIOR TO ANSWERING. I AM LOOKING FOR VERY SPECIFIC INFORMATION, THEREFORE PLEASE READ AND UNDERSTAND...
0
by: Jack | last post by:
Hi, I want to override an overridable method in some base class from my class. It works fine if I manually code the method (as one would expect), but I want the IDE to auto-generate the template...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...
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.