UP | HOME

Patterns: Factory method,Template method and Strategy

Basically, Factory method is a method that is defined in Parent class and is implemented in Child class, which usually is abstract method.

By contract, Template method is a method that implements an “algorithm”. This implementation is usually defined in a “Parent” class and leveraging other methods including Factory methods. (See sample below)

public abstract class PizzaStore {
        /**
         * #templateMethod(String type) is an Template Method.
         *

#+begin_html <p>
#+end_html

         * #factoryMethodOfInit() AND #factoryMethodOfCreate(String
type) are both
         * Factory Method.
         */
        public Pizza templateMethod(String type) {

                factoryMethodOfInit();

                Pizza pizza = factoryMethodOfCreate(type);

                System.out.println("--- Making a" + pizza.getName() + ”
---“);
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
                return pizza;
        }

        protected abstract void factoryMethodOfInit();
        protected abstract Pizza factoryMethodOfCreate(String type);
}

Here is a definition: Factory Method:    Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Template Method:    Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.    (Here “lets subclasses redefine certain steps” could be done by leveraging Factory method)

Relationship: Factory Methods are often called by template methods.

Then the Strategy Pattern, Spring DI uses this pattern IMO somehow.

Check the updated PizzaStore version below, londonPizzaCreator and nYorkPizzaCreator are “Strategies” of PizzaCreator. Then PizzaStore can change strategies at runtime regardless implementation detail by injecting various strategies. (via method setPizzaCreator)

public abstract class PizzaStore {

        private PizzaCreator pizzaCreator;

        public Pizza templateMethod(String type) {

                factoryMethodOfInit();

                Pizza pizza = pizzaCreator.createPizza(type);

                System.out.println(“— Making a” + pizza.getName() + ” —“);                 pizza.prepare();                 pizza.bake();                 pizza.cut();                 pizza.box();                 return pizza;         }         protected abstract void factoryMethodOfInit();         public void setPizzaCreator(PizzaCreator pizzaCreator) {                 this.pizzaCreator = pizzaCreator;         } }

//


“Strategies”

public interface PizzaCreator {         public Pizza createPizza(String type); }

public class londonPizzaCreator implements PizzaCreator {         public Pizza createPizza(String type) {                 // TODO add creat detail                 return null;         } }

public class nYorkPizzaCreator implements PizzaCreator {         public Pizza createPizza(String type) {                 // TODO add creat detail                 return null;         } }

Strategy:   Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Related to Template Method:   Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.

What’s more? Those patterns are actually in different category in the book by GoF, trying to understand the reason therefore to understand their diff more clearly.