Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

Object-Oriented Programming Template Method Pattern - PDF by tzk45278

VIEWS: 49 PAGES: 14

									Object-Oriented Programming
 Template Method Pattern




       CSIE Department, NTUT
           Woei-Kae Chen
  Template Method: Intent
Define the skeleton of an algorithm in
an operation, deferring some steps to
subclasses.
– Template Method lets subclasses redefine
  certain steps of an algorithm without
  changing the algorithm’s structure.
Template Method: Motivation (1)
 Application framework: Application and Document classes.
                             Application
 Document                    AddDocument()
                                                Template Method
 Save()               docs   OpenDocument()
 Open()                      DoCreateDocument()
 Close()                     CanOpenDocument()
 DoRead()                    AboutToOpenDocument()



 MyDocument                  MYApplication
  DoRead()                   DoCreateDocument()
                             CanOpenDocument()
                             AboutToOpenDocument()
     Factory Method

                             Return new MyDocument
   Template Method: Motivation (2)
                                                  Application
   Template Method
                                                  AddDocument()
                                                  OpenDocument()
void Application::OpenDocument(char * name)       DoCreateDocument()
 {                                                CanOpenDocument()
   if (!CanOpenDocument(name))                    AboutToOpenDocument()
       return; // can not handle this document
   Document *doc = DoCreateDocument();
   if (doc) {
                                                  MYApplication
       _doc-> AddDocument(doc);
        AboutToOpenDocument(doc);                 DoCreateDocument()
        doc->Open();                              CanOpenDocument()
        doc->DoRead();                            AboutToOpenDocument()
   }                         Concrete Operation
}
                                                  Return new MyDocument
Template Method: Motivation (3)
 OpenDocument() is a Template Method.
  – A template method defines an algorithm in
    terms of abstract operations that subclasses
    override to provide concrete behavior.
  – By defining abstract operations, the template
    method fixes their ordering, but it lets
    Application and Document subclasses vary
    those steps to suit their needs.
Template Method: Applicability
 The Template Method should be used
 – to implement the invariant parts of an
   algorithm once
     subclasses implement the behavior that can
     vary.
 – when common behavior among subclasses
   should be factored and localized in a
   common class to avoid code duplication.
 – To control subclasses extensions
     “hook” operations
Template Method: Structure
Abstract Class
                        …
TemplateMethod()        PrimitiveOperation1()
PrimitiveOperation1()   …
PrimitiveOperation2()   PrimitiveOperation2()
                        …



ConcreteClass

PrimitiveOptation1()
PrimitiveOptation2()
Template Method: Participants
 AbstractClass (Application).
 – Define abstract primitive operations that
   concrete subclasses define to implement
   steps of an algorithm.
 – Implements a template method defining
   the skeleton of an algorithm.
 ConcreteClass (MyApplication)
 – Implements the primitive operations to
   carry out subclass-specific steps of the
   algorithm.
Template Method: Collaboration

 ConcreteClass relies on
 AbstractClass to implement the
 invariant steps of the algorithm
Template Method: Consequence (1)
  A fundamental technique for code reuse.
  Hollywood principle: Don’t call us, we’ll call you.
  Template methods call:
   –   concrete operations (ConcreteClass or client)
   –   concrete AbstractClass operations
   –   primitive operations (must be overridden)
   –   factory methods
   –   hook operations (may be overridden)
          AbstractClass provides default behavior
Template Method: Consequence (2)
    Hook operation
     – Override is fine, but …
          It is easy to forget to call parent operation
   void ParentClass::Operation ( )
   {
        / / ParentClass behavior
   }

    void DerivedClass::Operation ( )
                                                      Easy to forget
    {
         ParentClass::Operation( );
         / / DerviedClass extended behavior
    }
Template Method: Consequence (3)
    Solution: Template Method
            void ParentClass::Operation ( )
            {                                    Template Method
  Always
                 / / ParentClass behavior
 executed        HookOperation( );
            }
            void ParentClass::HookOperation( )
            {
            }
            void DerivedClass: :HookOperation ( )
            {
                 / / derived class extension
            }
Template Method: Implementation
  Using C++ access control
  – primitive operations
       protected & virtual
       pure virtual (if not implemented)
  – template method
       non-virtual
  Minimizing primitive operations
  – so that subclasses can be implemented easily
  Naming conventions
  – add prefix to primitive operations (e.g.,
    DoCreateDocument(), DoRead())
Template Method: Related patterns
   Factory Methods
    – often called by template methods
    – example: DoCreateDocument()
   Strategy
    – Template Methods use inheritance to vary part
      of an algorithm.
    – Strategies use delegation to vary the algorithm.
    – Template Method may cooperate with Strategy
        The inheritance hierarchies of Strategy patterns may
        use Template Methods.

								
To top