Quantcast
Viewing all articles
Browse latest Browse all 10

Template method

Description:

Define the skeleton of the algorithm in an operation and deferring the exact implementations of the steps of the algorithms to its subclasses. Template method uses the HR policy of “we will call you” which means the exact implementations of the algorithm will be called by the base class.

Benefits:

  • Fundamental technique for reusing code.

When to use:

  • You want to implement the invariant parts of an algorithm once and use subclasses to implement the behavior that can vary.
  • When common behavior among subclasses should be factored and localized in a common class to avoid code duplication

Diagram:

Image may be NSFW.
Clik here to view.

Code sample:


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 10

Trending Articles