Infolinks In Text Ads

Strategy Design Pattern In Java

Strategy design pattern is one of the most use design pattern in real world Problems. In this post i am going to explain the intent , applicability, motivations and code example of of strategy design patterns.
Motivation:
Any run-time software system is composed of State (e.g. current value of instance variables)  and Behavior (e.g. how to borrow a book from library). A behavior is generally composed of several
  • low level algorithms (sub-behaviors)
  • For example, borrowing a book might include:
  •  Checking library enrollment of the requester
  • Checking borrowing policy for the requester 
  • Stamping book and issuing it
  • Sometimes you need to change these low level algorithms in multiple ways due to:
    – the type of user
    – the type of data
    – software features
Examples:
– Book borrow policy (different for student,professor)
– Game character motion behavior (different based on game conditions)
– Changing GUI outlook
– Changing sorting algorithms (Quicksort,Bubblesort, Mergesort, Heapsort etc)
– Changing game levels
Intent of strategy design pattern:
“Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy
lets the algorithm vary independently from clients that use it”
Applicability:
  • Many related classes differ in their behavior
          e.g Student, professor borrowing policy difference
  • Need different variants of an algorithm
         e.g Bubblesort, Quicksort, Heapsort
  • Hide algorithm complexity from clients
  • A class encompasses multiple behaviors (canbe spotted with multiple conditional statements in the class).
  • Require behavior pluggability
Participants:
Strategy:
declare interface common to all supported algorithms
ConcreteStrategy:
implement the algorithm using Strategy interface
Context:
–is configured with a ConcreteStrategy object
– maintains a reference to a Strategy object
– define interface that lets Strategy access its data

Stracture
 Example Code:
 Here we are going to explain strategy design pattern with Changing sorting algorithms (Quicksort,Bubblesort, Mergesort, Heapsort etc) example.

package aurangzeb.utk.com;

public interface SortInterface {
        public void sort(double[] list);
      }





package aurangzeb.utk.com;

public class BubbleSort implements SortInterface {
      public void sort(double[] list) {
            // Bubblesort algorithm here
      }
}



package aurangzeb.utk.com;

public class QuickSort implements SortInterface {
       
      public void sort(double[] u) {
          sort(u, 0, u.length - 1);
        }
       
        private void sort(double[] a, int left, int right) {
          if (right <= left) return;
          int i = part(a, left, right);
          sort(a, left, i-1);
          sort(a, i+1, right);
        }
       
        private int part(double[] a, int left, int right) {
          int i = left;
          int j = right;
          while (true) {
            while (a[i]< a[right])
              i++;
            while (smaller(a[right], a[--j]))
              if (j == left) break;
            if (i >= j) break;
            swap(a, i, j);
          }
          swap(a, i, right);
          return i;
        }
       
        private boolean smaller(double x, double y) {
          return (x < y);
        }
       
        private void swap(double[] a, int i, int j) {
          double swap = a[i]; a[i] = a[j]; a[j] = swap;
        }
}



package aurangzeb.utk.com;

public class SortingContext {
        private SortInterface sorter = null;
       
        public void sortDouble(double[] list) {
          sorter.sort(list);
        }
       
        public SortInterface getSorter() {
          return sorter;
        }
       
        public void setSorter(SortInterface sorter) {
          this.sorter = sorter;
        }
      }



package aurangzeb.utk.com;

public class SortingClient {
        public static void main(String[] args) {
          double[] list = {1,2.4,7.9,3.2,1.2,0.2,10.2,
              22.5,19.6,14,12,16,17};
          SortingContext context = new SortingContext();
          context.setSorter(new QuickSort());
          context.sortDouble(list);
          for(int i =0; i< list.length; i++) {
            System.out.println(list[i]);
          }
        }
      }

Newer Post Older Post

Leave a Reply

Powered by Blogger.