Follow treslines by email clicking Here!

Tuesday, August 12, 2014

Programming Design Pattern - Template Method Applied - Best Practise

Hi there!

Today i'm gonna show the template method design pattern in action. The template method design pattern is a very common when we are developing structures and base classes for a framework.

I'm assuming here you already know the concepts and i'll be focusing on practise. The example i will provide is a nice way to show it how it could looks like. You can always come back here, take it, adapt it and use it in your applictions as you may need. So be sure you bookmark it or join the group here on the right side of this post subscribing it.

First of all, let's take a look at the UML diagram of it. After that we will take the analogy for our example.

The UML diagram of the Template Method Pattern

Pay close attention, because once you understand that, everything will become clear and simple to understand. That's the reason I'm putting always the UML first. That way you'll get an eye for it with the time.


Why use it?

In our example we will develop a base class with some template methods for all derived concrete calculators. But what are template methods and why shall i use it? Well, think about some operations that must be implemented in a specific way. And you want to ensure it. That's a good reason for it. Another reason is to define the same contract for all instances. And finally because of the DRY principle. (Don't Repeat Yourself) The abstraction will put the code in a central place. This is good for maintance.

public abstract class CalculatorTemplate {
   public double multiply(double a, double b){
       double posA= getPositiveValueOf(a);
       double posB= getPositiveValueOf(b);
       return posA*posB;
   }
   public double substract(double a, double b){
       double posA= getPositiveValueOf(a);
       double posB= getPositiveValueOf(b);
       double greater = getGreaterValueOf(posA,posB);
       double smaller = getSmallerValueOf(posA,posB);
       return greater-smaller;
   }
   // 1. DEFINING THE TEMPLATES
   protected abstract double getSmallerValueOf(double a, double b);
   protected abstract double getGreaterValueOf(double a, double b);
   protected abstract double getPositiveValueOf(double n);
}

Concrete implementation

A further advantage of it is that the javadoc can be placed in the abstraction only once and must not be repeated also in the concrete implementations. Now let's say you want to implement a bunch of different looking calculators. With this abstraction you could it.

// 2. IMPLEMENTING THE TEMPLATES
public class Calculator extends CalculatorTemplate {
    @Override
    protected double getSmallerValueOf(double a, double b) {
        final int compareTo = Double.valueOf(a).compareTo(Double.valueOf(b));
        return compareTo > 0 ? a : b ;
    }
    @Override
    protected double getPositiveValueOf(double n) {
        return Double.valueOf(String.valueOf(n).replaceAll("-", ""));
    }
}

Testing it

Finally let's test it.
public class Client {
    public static void main(String[] args) {
        System.out.println("substraction: "+new Calculator().substract(50, -2));
        System.out.println("multiplication: "+new Calculator().multiply(-2, 50));
    }
}

That's all! I hope you like it!

No comments:

Post a Comment