- a string of numbers are sorted by choosing BubbleSort or QuickSort (factory pattern) from an interface window, decorated with 2 buttons (quick sort, bubble sort) and a label (where the result will be shown) - decorator pattern.
Here is the code for the window, the decorator pattern:
interface Window {
public void draw(); // draws the Window
public String getDescription(); // returns a description of the Window
public class decorator{
public static void main(String[] args) {
// create a decorated Window with horizontal and vertical scrollbars
Window decoratedWindow = new HorizontalScrollBarDecorator (new VerticalScrollBarDecorator(new SimpleWindow()));
// Window decoratedWindow2 = new SimpleWindow();
// print the Window's description
System.out.println(decoratedWindow.getDescription());
}
}
// abstract decorator class - note that it implements Window
abstract class WindowDecorator implements Window {
protected Window decoratedWindow; // the Window being decorated
public WindowDecorator (Window decoratedWindow) {
this.decoratedWindow = decoratedWindow;
}
}
// the first concrete decorator which adds vertical scrollbar functionality
class VerticalScrollBarDecorator extends WindowDecorator {
public VerticalScrollBarDecorator (Window decoratedWindow) {
super(decoratedWindow);
}
public void draw() {
drawVerticalScrollBar();
decoratedWindow.draw();
}
private void drawVerticalScrollBar() {
// draw the vertical scrollbar
}
public String getDescription() {
return decoratedWindow.getDescription() + ", including vertical scrollbars";
}
}
class SimpleWindow implements Window{
public void draw() {
//draw window
}
public String getDescription() {
return "simple window";
}
}
// the second concrete decorator which adds horizontal scrollbar functionality
class HorizontalScrollBarDecorator extends WindowDecorator {
public HorizontalScrollBarDecorator (Window decoratedWindow) {
super(decoratedWindow);
}
public void draw() {
drawHorizontalScrollBar();
decoratedWindow.draw();
decoratedWindow.setVisible(true);
}
private void drawHorizontalScrollBar() {
// draw the horizontal scrollbar
}
public String getDescription() {
return decoratedWindow.getDescription() + ", including horizontal scrollbars";
}
}
}
Instead of HorizontalScrollBarDecorator and VerticalScrollBarDecorator I will have ButtonBubbleSort and ButtonQuickSort and another one called ResultLabel. Thing is i don't know where to write down the code to actually show a window. I tried extending the JFrame class and using setsize() and setVisible(true) but in vain.
The second piece of code, the factory pattern is:
public interface SortInterface {
public void sort(double[] list);
public class QuickSort implements SortInterface {
public void sort(double[] a) {
quicksort(a, 0, a.length - 1);
}
private void quicksort(double[] a, int left, int right) {
if (right <= left) return;
int i = partition(a, left, right);
quicksort(a, left, i-1);
quicksort(a, i+1, right);
}
private int partition(double[] a, int left, int right) {
int i = left;
int j = right;
while (true) {
while (a[i]< a[right])
i++;
while (less(a[right], a[--j]))
if (j == left) break;
if (i >= j) break;
exch(a, i, j);
}
exch(a, i, right);
return i;
}
private boolean less(double x, double y) {
return (x < y);
}
private void exch(double[] a, int i, int j) {
double swap = a[i];
a[i] = a[j];
a[j] = swap;
}
}
public class BubbleSort implements SortInterface {
public void sort(double[] list) {
double temp;
for(int i = 0; i < list.length; i++) {
for(int j = 0; j < list.length - i; j++) {
if(list[i] < list[j]) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
}
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;
}
}
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 BubbleSort());
context.sortDouble(list);
for(int i =0; i< list.length; i++) {
System.out.println(list[i]);
}
}
}
}
Where do I need to add the code? How can i combine the two of them? Please make me understand..
Thx in advance.


Sign In
Create Account

Back to top









