I have a school project, I need to use two design patterns in my program. I decided to use the FACTORY and DECORATOR patterns like so:
- 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:
Code:
// the Window interface
interface Window {
public void draw(); // draws the Window
public String getDescription(); // returns a description of the Window
}
// implementation of a simple Window without any scrollbars
class SimpleWindow implements Window {
public void draw() {
// draw window
}
public String getDescription() {
return "simple window";
}
}
// 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";
}
}
// 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();
}
private void drawHorizontalScrollBar() {
// draw the horizontal scrollbar
}
public String getDescription() {
return decoratedWindow.getDescription() + ", including horizontal scrollbars";
}
}
public class DecoratedWindowTest {
public static void main(String[] args) {
// create a decorated Window with horizontal and vertical scrollbars
Window decoratedWindow = new HorizontalScrollBarDecorator (
new VerticalScrollBarDecorator(new SimpleWindow()));
// print the Window's description
System.out.println(decoratedWindow.getDescription());
}
}
Instead of HorizontalScrollBarDecorator and VerticalScrollBarDecorator I will have ButtonBubbleSort and ButtonQuickSort and another class called ResultLabel for printing the result of the sorters.
Thing is i don't know where to write down the code to actually show a window. I tried extending the SimpleWindow class to JFrame class and using setsize() and setVisible(true) but in vain.
The second piece of code, the factory pattern is:
Code:
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 to actually draw the window? How can i combine the two patterns? I've added setSize() setVisible(true) but it doesnt show the window frame... ofc i've also extended the class to JFrame and imported the right packages.. The decorator pattern template is off wiki, so here's a link maybe it can help:
Decorator pattern - Wikipedia, the free encyclopedia