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.
Printable View
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.
Quote:
Originally Posted by Peter_APIIT
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.
What exercise should i do ? What is the classical question i must do in order to understand this theory.Quote:
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.
For instance, one will write animal class to illustrate polymorphism. How about this theory ?
Are you mean not to using inheritance but using traits like STL did ?Quote:
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 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.
Quote:
Originally Posted by Peter_APIIT
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:
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 ReportGenerator
{
//...
public:
void doIt()
{
generateHeader();
generateBody();
generateTrailer();
}
protected:
virtual void generateHeader() = 0;
virtual void generateBody() = 0;
virtual void generateTrailer() = 0;
};
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:class PlainTextReportGenerator : public ReportGenerator
{
//...
protected:
virtual void generateHeader(){/* ... */}
virtual void generateBody(){/* ... */}
virtual void generateTrailer(){/* ... */}
};
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://In a real scenario you could obtain the generator from a factory.
ReportGenerator* generator = new PlainTextReportGenerator();
generator->doIt();
delete generator;
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.Code:template <class Generator_t>
class ReportGenerator
{
private:
Generator_t generator_;
public:
void doIt()
{
generator_.generateHeader();
generator_.generateBody();
generator_.generateTrailer();
}
};
Now, I guess it's a matter of creating/elaborating other situations and practicing... ;)
Well, good example.
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 ?
A billion thanks for your help.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;
You really a good person.
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.
Because using derivation allows you to vary the action at run-time, whereas a template is fixed at compile time.Quote:
Originally Posted by Peter_APIIT
My understanding is like this.Quote:
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.
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?
The technology behind is polymorphism.Quote:
Because using derivation allows you to vary the action at run-time, whereas a template is fixed at compile time.
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.
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.
The base class function defines the algorithm, the derived classes fill in the blanks.Quote:
Originally Posted by Peter_APIIT
I'm not sure what you mean by SAP ABAP.Quote:
Originally Posted by Peter_APIIT
This link may help.
http://en.wikipedia.org/wiki/ABAP
Thanks.
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.
Yes, it calls the virtual functions that are overriden in PlainTextReportGenerator. You could have tested this yourself.Quote:
Originally Posted by Peter_APIIT
Now the problem is 2. How i can write a report generator ?
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();
}
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.