Strategy Design Pattern by example

This post explains Strategy design pattern using an example.

When to Use ? – Its an alternative to Template Design Pattern. Use this pattern when template methods implementation as well as steps in which they need to execute are dynamic. Its like no of steps in algorithm vary or the class that implements the steps needs as independent inheritance hierarchy .

How to use ? – Implement an interface defining the individual steps. Define a class that implements this interface ( individual steps) , this class is not forced to inherit from an abstract template super class. Main algorithm implementation class uses this interface implementation for each of algorithm steps execution

An example – Same example as Template Method Design pattern is being used, Process order.

Interface -

package strategypattern;

import java.util.List;
import templatemethod.Order;

public interface ProcessOrderHelper {
    public  boolean  validateCustomer(String custName, int custId);
    public  boolean checkAvailibility(List items);
    public  boolean checkShipping(String shippingAdd);
    public  boolean generateInvoice(Order order);
    public  boolean sendNotification();
    public  boolean placeOrder(Order order);
}

This interface include all template methods which are being used in algorithm.

Implementation -

package strategypattern;

import java.util.List;

import templatemethod.Order;

public class ProcessOrderHelperImpl implements ProcessOrderHelper {
    public ProcessOrderHelperImpl() {
    }

    public boolean validateCustomer(String custName, int custId) {
        //...
        return true;
    }

    public boolean checkAvailibility(List items) {
        //...
        return true;
    }

    public boolean checkShipping(String shippingAdd) {
        //...
        return true;
    }

    public boolean generateInvoice(Order order) {
        //...
        return true;
    }

    public boolean sendNotification() {
        //...
        return true;
    }

    public boolean placeOrder(Order order) {
        //...
        return true;
    }
}

This is the implementation for the interface.

Class implementing Algorithm -

package strategypattern;

import templatemethod.Order;

public class ProcessOrder {
    private ProcessOrderHelper helper = null;
    public ProcessOrder() {
    }

    public void setHelper(ProcessOrderHelper helper) {
        this.helper = helper;
    }

    public ProcessOrderHelper getHelper() {
        return helper;
    }
    public final String processOrder(Order order){
        String status = "TRUE";
        if(helper.validateCustomer(order.getCustomerName(),order.getCustomerId())){
            status = "Customer Information not Found";
        }
        if(helper.checkAvailibility(order.getItems())){
            status="Items are not available";
        }
        if(helper.checkShipping(order.getShipAdd())){
            status="Shipping is not available";
        }
        helper.generateInvoice(order);
        helper.placeOrder(order);
        helper.sendNotification();
        return status;
    }
}

       When to prefer Strategy design pattern over Template Method design pattern ? -  When there are many different implementation of the steps, or what it’s expected that the number of implementations will continue to increase. In this case flexibility of the Strategy pattern will almost certainly prove beneficial, as it allows maximum freedom to the implementation.

Comments

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester