5

is this an example of TemplateMethod Pattern??

public abstract class Character{

    public final void useWeapon(){
        useBusterSword();
        useMateriaBlade();
        useUltimateWeapon();
    }

    public abstract void useBusterSword();
    public abstract void useMateriaBlade();
    public abstract void useUltimateWeapon();
}

public class Cloud extends Character{

    public void useUltimateWeapon() {
        System.out.println("Change Weapon to Ultima Weapon");
    }


    public void useBusterSword() {

    }


    public void useMateriaBlade() {

    }
}


public class TestGame {
    public static void main(String[] args){
        Character cloud = new Cloud();
        cloud.useWeapon();
    }
}

If so then what is the advantage of using this pattern than strategy pattern??

Strategy Pattern

public class Character {
    WeaponBehavior w;
    public void setWeaponBehavior(WeaponBehavior wb){
        w = wb;
    }

    public void useWeapon(){
        w.useWeapon();
    }
}

public class Cloud extends Character{

    public Cloud(){
        w = new UltimaWeapon();
    }

}


public interface WeaponBehavior {
    public void useWeapon();
}

public class UltimaWeapon implements WeaponBehavior {
    public void useWeapon() {
        System.out.println("Change Weapon to UltimaWeapon");
    }

}

public class BusterSword implements WeaponBehavior {
    public void useWeapon() {
        System.out.println("Change Weapon to MateriaBlade");
    }

}

public class MateriaBlade implements WeaponBehavior {
    public void useWeapon() {
        System.out.println("Change Weapon to MateriaBlade");
    }

}

public class TestGame {
    public static void main(String[] args){
        Character c = new Cloud();
        c.useWeapon();
    }
}

What I noticed is Strategy pattern encapsulate what varies unlike TemplateMethod Pattern lets the subclassed handles what varies.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
user1364686
  • 63
  • 2
  • 7

4 Answers4

3

Strategy pattern defines a family of algorithms and makes them interchangeable. Client code can use different algorithms since the algorithms are encapsulated.

Template method defines the outline of an algorithm and lets subclasses part of the algorithm's implementation. So you can have different implementations of an algorithms steps but retain the algorithm's structure

So as you can see the intent is different in each pattern. So it is not a matter of advantage of one over the other.

Cratylus
  • 52,998
  • 69
  • 209
  • 339
2

Yes you can use strategy pattern instead of template method but in special case you will repeat algorithm logic.

The best use case for template method to replace some abstract method by specific implementation.

E.g. You want to cook smth and your recipe is:

  1. take ingridients
  2. put them into
  3. heat
  4. give coocked food to smbd

In java recipe is just method in some Recipe class

void cook(){
  takeIngridients();
  putIt();  // abstract
  heat();  //abstract
  giveFood();
}

You create PanRecipe class extends Recipe and implement abstract methods to use pan. Another class can be GrillRecipe and implement methods to use grill. Now you can just call it by grillRecipe.cook(), and instead of strategy pattern don't need to copy implementation of repeated methods takeIngridients and giveFood.

mishadoff
  • 10,719
  • 2
  • 33
  • 55
1

Template method pattern is useful when you want to use some parent class's fields and when your implementation is not really a whole algorithm but only some 'logic' very specific to your hierarchy of classes.

On the other hand, when you find out that your template methods implementations are redundant or leads to duplication code across several subclasses of the same tree, prefer Strategy pattern so that your code will be factorized.

Also, template method working by the way of subclassing, you can't change your behaviour at runtime whereas with Strategy pattern, all you have to do is to use setter to change your behaviour whenever you wish.

In all other cases, those two patterns are very similar and you can often choose for the one you like.

Mik378
  • 21,881
  • 15
  • 82
  • 180
0

Similarities:

  1. Both Template method & Strategy are behavioural patterns.
  2. Both patterns are used to change an algorithm by sub classes but with a difference - Partial or full

For better understanding of these two features, we have to understand core differences in implementation of these two patterns.

Core Differences:

  1. Template method uses Inheritance and Strategy uses composition
  2. The Template method implemented by the base class should not be overridden. In this way, the structure of the algorithm is controlled by the super class, and the details are implemented in the sub classes
  3. Strategy encapsulates the algorithm behind an interface, which provide us ability to change the algorithm at run time

Related posts:

Real World Example of the Strategy Pattern

Template design pattern in JDK, could not find a method defining set of methods to be executed in order

Have a look at Template method and Strategy articles for better understanding.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211