Java Code Samples

Template method

Posted in Old stuff by Cristian on October 5, 2011

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:

Code sample:

About these ads

One Response

Subscribe to comments with RSS.

  1. […] Template method […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: