-
August 26th, 2008, 06:18 AM
#1
Template Method Question
Hello to all expert, i wonder do you have any advise that what exercise i should do in order to sharpen my skills for template method design pattern.
I know the theory which sub class redefine partial algorithm.
Thanks for your help.
-
August 26th, 2008, 06:45 AM
#2
Re: Template Method Question
Originally Posted by Peter_APIIT
Hello to all expert, i wonder do you have any advise that what exercise i should do in order to sharpen my skills for template method design pattern.
I know the theory which sub class redefine partial algorithm.
Hi.
You don't need to use a sub class to implement the template method pattern. You can use C++ templates - I'm pretty sure this is mentioned in GoF's book. In fact, I consider this a better approach in most cases (no runtime overhead due to eventual virtual table pointers).
I guess the best way to get it in practice is to try to write a simple application the uses the concepts. Thinking and elaborating on it is part of the process.
You might also wanna take a look at policy based design, which is kind of a variation of the Strategy design pattern using templates.
-
August 26th, 2008, 10:12 PM
#3
Re: Template Method Question
I guess the best way to get it in practice is to try to write a simple application the uses the concepts. Thinking and elaborating on it is part of the process.
What exercise should i do ? What is the classical question i must do in order to understand this theory.
For instance, one will write animal class to illustrate polymorphism. How about this theory ?
You don't need to use a sub class to implement the template method pattern. You can use C++ templates - I'm pretty sure this is mentioned in GoF's book. In fact, I consider this a better approach in most cases (no runtime overhead due to eventual virtual table pointers).
Are you mean not to using inheritance but using traits like STL did ?
I not interested in policy based design because it is compile time. Lack of flexibility.
What is the benefits using policy based design?
By the way, i more concern to template method right now.
Thanks for your help.
Last edited by Peter_APIIT; August 26th, 2008 at 10:14 PM.
Reason: Add some information
-
August 27th, 2008, 06:59 AM
#4
Re: Template Method Question
Originally Posted by Peter_APIIT
What exercise should i do ? ... How about this theory ?
I not interested in policy based design because it is compile time. Lack of flexibility.
..
What is the benefits using policy based design?
Ok, let me suggest you an example then. Suppose you're writing a report generation tool and you have an abstract class ReportGenerator structured like this:
Code:
class ReportGenerator
{
//...
public:
void doIt()
{
generateHeader();
generateBody();
generateTrailer();
}
protected:
virtual void generateHeader() = 0;
virtual void generateBody() = 0;
virtual void generateTrailer() = 0;
};
You can have different kinds of generators. One could generate plain text reports, other a XML report and a third a PDF report. Each one of them is supposed to implement the three generate functions. For example:
Code:
class PlainTextReportGenerator : public ReportGenerator
{
//...
protected:
virtual void generateHeader(){/* ... */}
virtual void generateBody(){/* ... */}
virtual void generateTrailer(){/* ... */}
};
Now, you're able to obtain a particular generator through a reference to ReportGenerator and call the doIt method, which defines the "template" of the generate algorithm.
Code:
//In a real scenario you could obtain the generator from a factory.
ReportGenerator* generator = new PlainTextReportGenerator();
generator->doIt();
delete generator;
The option I mentioned about using C++ templates is very similar. However, you'll loose the ability to set the desired generator at runtime (this is what you probably mentioned as "lack of flexibility"). On the other hand you'll get better performance. (The template approach is usually better suited if you're building a software library.)
Code:
template <class Generator_t>
class ReportGenerator
{
private:
Generator_t generator_;
public:
void doIt()
{
generator_.generateHeader();
generator_.generateBody();
generator_.generateTrailer();
}
};
I also reminded you of policy based design because it's also an alternative for providing generic implementations for specified behaviors. If you take a look at the link I pointed in the first post, you'll get the feeling.
Now, I guess it's a matter of creating/elaborating other situations and practicing...
-
August 28th, 2008, 05:25 AM
#5
Re: Template Method Question
-
November 27th, 2008, 11:30 PM
#6
Re: Template Method Question
There are three problems concern me at the moment.
1. How to call doIt from derived class or i have delcare doIT in every derived class ?
As you said, you using pointer to call DoIt at reportGenerator but doIt called pure virtual function which doesn't have any function definition which redefined by derived class.
2. Why you said using C++ template in template method will lost flexibility ?
3. How to write the three generate function ?
Code:
class ReportGenerator
{
//...
public:
void doIt() -> Template Method
{
generateHeader();
generateBody();
generateTrailer();
}
protected: ->Primitive Operations
virtual void generateHeader() = 0;
virtual void generateBody() = 0;
virtual void generateTrailer() = 0;
};
class PlainTextReportGenerator : public ReportGenerator
{
//...
protected:
virtual void generateHeader(){/* ... */}
virtual void generateBody(){/* ... */}
virtual void generateTrailer(){/* ... */}
};
//In a real scenario you could obtain the generator from a factory.
ReportGenerator* generator = new PlainTextReportGenerator();
generator->doIt();
delete generator;
A billion thanks for your help.
You really a good person.
Thanks for your help.
-
November 28th, 2008, 04:04 AM
#7
Re: Template Method Question
Originally Posted by Peter_APIIT
There are three problems concern me at the moment.
1. How to call doIt from derived class or i have delcare doIT in every derived class ?
As you said, you using pointer to call DoIt at reportGenerator but doIt called pure virtual function which doesn't have any function definition which redefined by derived class.
No, doIt only exists in the base (enclosing) class. There is only one version of doIt, and that's the one that's always called - this is the essence of the template pattern: some portion of the algorithm is fixed, mandatory, whilst other parts are flexible.
Originally Posted by Peter_APIIT
2. Why you said using C++ template in template method will lost flexibility ?
Because using derivation allows you to vary the action at run-time, whereas a template is fixed at compile time.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 29th, 2008, 12:48 AM
#8
Re: Template Method Question
No, doIt only exists in the base (enclosing) class. There is only one version of doIt, and that's the one that's always called - this is the essence of the template pattern: some portion of the algorithm is fixed, mandatory, whilst other parts are flexible.
My understanding is like this.
1. Since the base class is abstract base class, therefore we can called doIt using pointer but in case base class is not abstract class, then how we called doIt?
2. We can called different version of primitive function based on object for instance, PlainTextReportGenerator object actually called its member function(generateHeader) based on polymorhism. Then XML report and a third a PDF report also called its member function(generateHeader) through doIt.
Am i correct ?
Please correct me if i wrong.
3. Since we not using SAP ABAP, how we can code the generateHeader function?
Because using derivation allows you to vary the action at run-time, whereas a template is fixed at compile time.
The technology behind is polymorphism.
I know that template metaprogramming is generated code during compilation. Any concrete example how it fixed at compile time?
Are you talking that evaluation of object content is happen at run time through polymorphism and evaluation of object content happen at compile time ?Sorry for my poor English.
Please help me.
Thanks for your help.
Last edited by Peter_APIIT; November 29th, 2008 at 12:53 AM.
Thanks for your help.
-
November 29th, 2008, 05:58 AM
#9
Re: Template Method Question
Originally Posted by Peter_APIIT
My understanding is like this.
1. Since the base class is abstract base class, therefore we can called doIt using pointer but in case base class is not abstract class, then how we called doIt?
Just call it. There is no problem here, whatever the base class is: just call the function, whether through a pointer to base, a pointer to derived, a derived object (if base is abstract) or a base object (if it isn't). Template pattern pretty much demands that the base be abstract, since it doesn't make much sense if you don't derive from it and override the virtual functions, though.
Just call it.
Originally Posted by Peter_APIIT
2. We can called different version of primitive function based on object for instance, PlainTextReportGenerator object actually called its member function(generateHeader) based on polymorhism. Then XML report and a third a PDF report also called its member function(generateHeader) through doIt.
Am i correct ?
Please correct me if i wrong.
The base class function defines the algorithm, the derived classes fill in the blanks.
Originally Posted by Peter_APIIT
3. Since we not using SAP ABAP, how we can code the generateHeader function?
The technology behind is polymorphism.
I know that template metaprogramming is generated code during compilation. Any concrete example how it fixed at compile time?
Are you talking that evaluation of object content is happen at run time through polymorphism and evaluation of object content happen at compile time ?Sorry for my poor English.
Please help me.
Thanks for your help.
I'm not sure what you mean by SAP ABAP.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 30th, 2008, 09:53 PM
#10
Re: Template Method Question
Thanks for your help.
-
November 30th, 2008, 09:59 PM
#11
Re: Template Method Question
My last two question is
1. If i have this :
ReportGenerator* generator = new PlainTextReportGenerator();
generator->doIt();
delete generator;
Does C++ called the PlainTextReportGenerator() virtual fnction from DoIt() ?
2. How i can write a report generator ?
Thanks.
Thanks for your help.
-
December 1st, 2008, 01:03 AM
#12
Re: Template Method Question
Originally Posted by Peter_APIIT
Does C++ called the PlainTextReportGenerator() virtual fnction from DoIt() ?
Yes, it calls the virtual functions that are overriden in PlainTextReportGenerator. You could have tested this yourself.
-
December 1st, 2008, 03:31 AM
#13
Re: Template Method Question
Now the problem is 2. How i can write a report generator ?
Thanks for your help.
-
December 1st, 2008, 03:50 AM
#14
Re: Template Method Question
Here's a quick example.
Code:
class ReportGenerator
{
//...
public:
void doIt()
{
generateHeader();
generateBody();
generateTrailer();
}
private:
virtual void generateHeader() = 0;
virtual void generateBody() = 0;
virtual void generateTrailer() = 0;
};
class PlainTextReportGenerator: public ReportGenerator
{
private:
virtual void generateHeader()
{
//Something goes here
cout << "generateHeader" << '\n';
}
virtual void generateBody()
{
//Something goes here
cout << "generateBody" << '\n';
}
virtual void generateTrailer()
{
//Something goes here
cout << "generateTrailer" << '\n';
}
};
int main()
{
ReportGenerator *g = new PlainTextReportGenerator();
g->doIt();
}
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
December 2nd, 2008, 10:13 PM
#15
Re: Template Method Question
I want to write algorithm for generate member function.
Why base class undefined when my PlainTextReportGenerator is in different header file ?
class ReportGenerator is in ReportGenerator.h
class PlainTextReportGenerator is in PlainTex.h
Thanks for your help.
Last edited by Peter_APIIT; December 3rd, 2008 at 03:23 AM.
Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|