The Template Method Design Pattern

The template method design pattern defines a generic algorithm and specify hooks for plugging in extension points and variations. How does this fit in with systematic reuse? It supports two levels of reuse: reuse the overall template algorithm over and over. Basically, an entire set of steps that gets repeated as-is or reuse specific steps across one or more template algorithms.

Template method can be used in a variety of scenarios. For example, the following steps could be performed for handling requests:

  • validate input request using security token
  • standardize data – remove whitespaces, unify upper/lower case, correct misspelling
  • pre-processing e.g. transform request prior to processing
  • processing step – e.g. executing specific business logic
  • post processing e.g. transform response message in a specific format, encrypt sensitive data

Key question here: what are the variations in the above steps?
– authenticating requests – using a LDAP provider vs. using X509 certificates
– standardizing data – use custom implementation or open source tools to fix request data and perform pattern matching
– transformation: might vary by format (XML, plain text, JSON etc.)
– processing: might vary by use case

The security module itself could be used by other apps requiring authentication. the transformation module could be used for a batch process and processing logic could be reusable for more than one project as well. As always, it is key to keep the template algorithm simple, cohesive, and loosely coupled via well designed application programming interfaces.

Like this post? Subscribe to RSS feed or get blog updates via email.

tweet this add to post to facebook

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: