Ideas, Knowledge, Technology, Computer Science, Experience associated with my work and some geeky stuff I progressively encounter during my journey towards enlightenment. Read on…

  • RSS RSS Feed

    • The Pragmatic Programmer
      I finished reading The Pragmatic Programmer by Andrew Hunt and David Thomas. It’s not a new book in the market but I was curious to read this. The technology topics covered, are not any different from those found in most software engineering books, but the way they’re presented using Pragmatic Philosophy Approach, is remarkable. Code […]
    • 2013 in review
      The stats helper monkeys prepared a 2013 annual report for this blog. Here’s an excerpt: A San Francisco cable car holds 60 people. This blog was viewed about 1,200 times in 2013. If it were a cable car, it would take about 20 trips to carry that many people. Click here to see the […]
    • Goodbye, Ness!
      It had to happen sometime. I thought Feb 2013 was the right time. I quit Ness after a long 5 years and 4 months of stay, in Feb. I joined FICO (formerly, Fair Isaac) last Feb.  While I get an opportunity to work with many varied stakeholders like Scientists, Architect, Product Management, Peer Developers, PMO, Technical Publications and also […]
    • Meta: information retrieval from Tweets
      I pick significant problems randomly sometimes and enjoy solving them, or at least attempt designing api :-). Here’s one such problem! Problem: How’d you go about finding meta information about a person’s tweets? NOTE: a) Tweet == Twitter updates b) Meta information –> Loosely defined. You can interpret it anyway you want –> Frequency, topics, follower […]
    • Understanding Big Data
      It’s been a while, since I last posted! To keep this rolling (I’m hardly getting any time to post my own articles or stuff about my experiments these days 😦  ), I just wanted to share this ebook on Big Data titled  Understanding Big Data: Analytics for Enterprise Class and Streaming Data. Cheers!
  • Twitter Updates

Java Design Patterns: Template Pattern in the Real World

Posted by sanstechbytes on July 31, 2012

If you’re declaring some abstract methods in your class and making the sub classes provide concrete implementation for them, you’re indeed using Template pattern. Template pattern formalizes the idea of defining an algorithm in a class, but leaving some of the details to be implemented in sub classes.

In Java, Arrays.sort(Object[] o) internally invokes mergesort, that in turn, does a .compareTo() check for object comparison. .compareTo() implementation for the object type being compared, will be invoked.  AbstractCollection is one example, too.

Suppose, you’re designing a Sort API that you intend to ship as a jar file to your customer. Your customer would like the flexibility of adding newer or better sort implementations (specialized sorts are needed in a large scale system, applications) in their code. Often, you wouldn’t want to invoke default sort implementation of Java for large data. For instance, your customer wants to sort a very large array of 0’s and 1’s. They would write their own sort logic yielding in-place, O(n) time-complexity, rather than use default Arrays.sort(..) with O( n log n) in time. Obviously, they don’t want you to modify code for them every time. You want details of sort to be implemented in the sub classes that your customer writes. You would come up with something like this below:

public abstract class Sorter {
 public void sort(Object[] o) {

 abstract void sort1(Object[] o);

class CustomSorter1 extends Sorter {

 public CustomSorter1() {


 public void sort(Object[] o) {

 //Your sorting logic goes here



Suppose, you’re developing a Graphics project for school. One of the tasks is to, drawing different triangles based on the input points. You would have subclasses like IsosCelesTriangle, EquilateralTriangle,  an abstract class Triangle and Graphics and Point classes.  Graphics and Point will be composite objects of Triangle.  drawLine() is common to all Triangle types. draw2ndLine() methods will be different for each Traingle type and implemented by sub classes.

Suppose, you’re building a Tourist Trip Reservation application. In every package trip, the activities are defined for each day of a Trip. Depending on the package type, the activities might vary. If you apply Template Pattern here: PackageTrip class is the abstract class. PackageA, PackageB etc are sub classes. performTrip() will be called by PackageA, PackageB etc. It would internally invoke specific Package implementations.

Template patterns is a one of the very useful and frequently used design patterns. Think about scenarios where you could apply in your project, now!


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 )

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

%d bloggers like this: