Tuesday, May 29, 2012

Design Pattern - Template Method

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

Intent:
  1. Define the skeleton of an algorithm in an operation, deferring some steps to subclasses and let subclasses redefine steps of an algorithm without changing the algorithm's structure.
Applicability:
  1. To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behaviro that can vary.
  2. Avoid code duplication.
  3. To control subclasses exensions.
Structure:

Participants:
AbstractClass
  1. defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
  2. implements a template method defining the skeleton of an algorithm.
  3. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
ConcreteClass
  1. implements the primitive opreations to carry out subclass-specific steps of the algorithm.
Overview:
ConcreteClass relies on AbstractClass to implement the invariant steps of the algorithm
  1. Reuses code
  2. Factors common behavior.
  3. Realizes "HR" principle.
Applications:
  1. Application
  2. MyApplication
  3. Print Document
  4. Manufacture
  5. Approve loan:
    1. bank history
    2. credit score from credit card companies
    3. other loan history
    4. evaluate assets
    5. income in the future
  6. Make Pizza
    1. take order & money
    2. send order to kitchen
    3. receive pizza from kitchen
    4. deliver pizza
    5. clean table

No comments: