CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Template Method Question

    Quote 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.

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Template Method Question

    Quote 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...

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Template Method Question

    Well, good example.

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Template Method Question

    Quote Originally Posted by Peter_APIIT View Post
    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.

    Quote 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


  8. #8
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Template Method Question

    Quote Originally Posted by Peter_APIIT View Post
    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.
    Quote 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.
    Quote 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


  10. #10
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Template Method Question

    This link may help.

    http://en.wikipedia.org/wiki/ABAP

    Thanks.
    Thanks for your help.

  11. #11
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Template Method Question

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Template Method Question

    Now the problem is 2. How i can write a report generator ?
    Thanks for your help.

  14. #14
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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.

  15. #15
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured