Chapter 8, Design Patterns Factory
A Pattern Taxonomy Pattern Behavioral Creational Structural Pattern Composite Decorator Adapter Bridge Façade Proxy Iterator Command Observer Template Strategy Singleton Abstract Factory Builder Factory Prototype
Animated Sorting Algorithms These programs will display multiple sorting routines running in parallel !! The primary design method will be a factory There will be a factory that produces different sorting routines Another factory will produce different sorting displays The algorithms will be developed through several iterations to illustrate these components Run Those Sorts!
Initial Version The first attempt uses the Template design pattern; the methods are The subclasses define the following methods
Creating Random Data protected void scramble() { arr = new int[getSize().height / 2]; for (int i = arr.length; --i >= 0;) { arr[i] = i; } int j = (int)(i * Math.random()); swap(arr, i, j); This swap routine will be used by the sorting methods private void swap(int a[], int i, int j) { int T; T = a[i]; a[i] = a[j]; a[j] = T;
The paintFrame Routine The integer value will control the height of the line that is drawn protected void paintFrame(Graphics g) { Dimension d = getSize(); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height); g.setColor(Color.black); int y = d.height - 1; double f = d.width / (double) arr.length; for (int i = arr.length; --i >= 0; y -= 2) g.drawLine(0, y, (int)(arr[i] * f), y); }
One Sorting Routine protected void MYSTERY(int a[]) { for (int i = a.length; --i >= 0; ) for (int j = 0; j < i; j++) { if (a[j] > a[j+1]) swap(a, j, j + 1); pause(); } Solve the MYSTERY; what name really belongs here for this sort? What is the complexity of this sort if there are n items The code for this sort is short, so does that mean it will run quickly?
protected void quickSort(int a[], int lo0, int hi0) { int lo = lo0; int hi = hi0; int mid; pause(); if (hi0 > lo0) { mid = a[(lo0 + hi0) / 2 ]; while(lo <= hi) { while ((lo < hi0) && (a[lo] < mid)) ++lo; while ((hi > lo0) && (a[hi] > mid)) --hi; if(lo <= hi) { swap(a, lo, hi); pause(); ++lo; --hi; } if(lo0 < hi) quickSort(a, lo0, hi); if(lo < hi0) quickSort(a, lo, hi0); Here is Quicksort
The algorithm Method and Initialization protected void algorithm() { if ("BubbleSort".equals(algName)) bubbleSort(arr); else if ("QuickSort".equals(algName)) quickSort(arr, 0, arr.length - 1); else } protected void initAnimator() { setDelay(20); algName = "BubbleSort"; String at = getParameter("alg"); if (at != null) algName = at; scramble();
Using the Strategy Design Pattern The class relations This sort() method is abstract
The Sort Subclasses class BubbleSortAlgorithm extends SortAlgorithm { void sort(int a[]) { for (int i = a.length; --i>=0; ) for (int j = 0; j<i; j++) { if (a[j] > a[j+1]) swap(a, j, j+1); pause(); } public BubbleSortAlgorithm(AlgorithmAnimator animator) { super(animator); Carefully describe the changes that are needed for the Quicksort subclass
public class QuickSortAlgorithm extends SortAlgorithm { public QuickSortAlgorithm(AlgorithmAnimator animator) { super(animator); } protected void QSort(int a[], int lo0, int hi0) { // the partition process goes here if( lo0 < hi ) QSort( a, lo0, hi ); if( lo < hi0 ) QSort( a, lo, hi0 ); public void sort(int a[]) { QSort(a, 0, a.length - 1);
Creating the Concrete Algorithms Run Sort2
Using an Algorithm Factory
The Interface and Implementation public interface SortAlgorithmFactory { SortAlgorithm makeSortAlgorithm(String algName); } public class StaticAlgoFactory implements SortAlgorithmFactory { public SortAlgorithm makeSortAlgorithm(String algName) { if ("BubbleSort".equals(algName)) return new BubbleSortAlgorithm(animator); else if ("QuickSort".equals(algName)) return new QuickSortAlgorithm(animator); else protected AlgorithmAnimator animator; public StaticAlgoFactory(AlgorithmAnimator animator) { this.animator = animator;
Sort2 Revised public class Sort2 extends AlgorithmAnimator { public void initAnimator() { setDelay(20); algName = "BubbleSort"; String at = getParameter("alg"); if (at != null) algName = at; algorithmFactory = new StaticAlgoFactory(this); theAlgorithm =algorithmFactory.makeSortAlgorithm(algName); scramble(); } protected void algorithm() { if (theAlgorithm != null) theAlgorithm.sort(arr); protected void scramble() // same as before protected void paintFrame(Graphics g) // same as before protected int arr[]; protected String algName; protected SortAlgorithm theAlgorithm; protected SortAlgorithmFactory algorithmFactory; Sort2 Revised
The Factory Design Pattern - 1
The Factory Design Pattern - 2