Using UML Patterns and Java ObjectOriented Software Engineering
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Design Patterns Factory
Pattern A Pattern Taxonomy Structural Pattern Behavioral Pattern Creational 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 !! Run Those Sorts! • 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
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[get. Size(). height / 2]; for (int i = arr. length; --i >= 0; ) { arr[i] = i; } for (int i = arr. length; --i >= 0; ) { 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 paint. Frame Routine • The integer value will control the height of the line that is drawn protected void paint. Frame(Graphics g) { Dimension d = get. Size(); g. set. Color(Color. white); g. fill. Rect(0, 0, d. width, d. height); g. set. Color(Color. black); int y = d. height - 1; double f = d. width / (double) arr. length; for (int i = arr. length; --i >= 0; y -= 2) g. draw. Line(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 quick. Sort(int a[], int lo 0, int hi 0) { int lo = lo 0; int hi = hi 0; int mid; pause(); if (hi 0 > lo 0) { mid = a[(lo 0 + hi 0) / 2 ]; while(lo <= hi) { while ((lo < hi 0) && (a[lo] < mid)) ++lo; while ((hi > lo 0) && (a[hi] > mid)) --hi; if(lo <= hi) { swap(a, lo, hi); pause(); ++lo; --hi; } } if(lo 0 < hi) quick. Sort(a, lo 0, hi); if(lo < hi 0) quick. Sort(a, lo, hi 0); } } Here is Quicksort
The algorithm Method and Initialization protected void algorithm() { if ("Bubble. Sort". equals(alg. Name)) bubble. Sort(arr); else if ("Quick. Sort". equals(alg. Name)) quick. Sort(arr, 0, arr. length - 1); else bubble. Sort(arr); } protected void init. Animator() { set. Delay(20); alg. Name = "Bubble. Sort"; String at = get. Parameter("alg"); if (at != null) alg. Name = at; scramble(); }
Using the Strategy Design Pattern • The class relations This sort() method is abstract
The Sort Subclasses class Bubble. Sort. Algorithm extends Sort. Algorithm { 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 Bubble. Sort. Algorithm(Algorithm. Animator animator) { super(animator); } } • Carefully describe the changes that are needed for the Quicksort subclass
public class Quick. Sort. Algorithm extends Sort. Algorithm { public Quick. Sort. Algorithm(Algorithm. Animator animator) { super(animator); } protected void QSort(int a[], int lo 0, int hi 0) { // the partition process goes here if( lo 0 < hi ) QSort( a, lo 0, hi ); if( lo < hi 0 ) QSort( a, lo, hi 0 ); } } public void sort(int a[]) { QSort(a, 0, a. length - 1); } }
Creating the Concrete Algorithms Run Sort 2
Using an Algorithm Factory
The Interface and Implementation public interface Sort. Algorithm. Factory { Sort. Algorithm make. Sort. Algorithm(String alg. Name); } public class Static. Algo. Factory implements Sort. Algorithm. Factory { public Sort. Algorithm make. Sort. Algorithm(String alg. Name) { if ("Bubble. Sort". equals(alg. Name)) return new Bubble. Sort. Algorithm(animator); else if ("Quick. Sort". equals(alg. Name)) return new Quick. Sort. Algorithm(animator); else return new Bubble. Sort. Algorithm(animator); } protected Algorithm. Animator animator; public Static. Algo. Factory(Algorithm. Animator animator) { this. animator = animator; } }
public class Sort 2 extends Algorithm. Animator { public void init. Animator() { set. Delay(20); alg. Name = "Bubble. Sort"; String at = get. Parameter("alg"); if (at != null) alg. Name = at; algorithm. Factory = new Static. Algo. Factory(this); the. Algorithm =algorithm. Factory. make. Sort. Algorithm(alg. Name); scramble(); } protected void algorithm() { if (the. Algorithm != null) the. Algorithm. sort(arr); } protected void scramble() // same as before protected void paint. Frame(Graphics g) // same as before protected int arr[]; protected String alg. Name; protected Sort. Algorithm the. Algorithm; protected Sort. Algorithm. Factory algorithm. Factory; } Sort 2 Revised
The Factory Design Pattern - 1
The Factory Design Pattern - 2
- Slides: 18