Decorator+Factory pattern. Need help!! menu

User Tag List

Results 1 to 1 of 1
  1. #1
    florinb's Avatar Member
    Reputation
    1
    Join Date
    Nov 2008
    Posts
    1
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Decorator+Factory pattern. Need help!!

    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

    Decorator+Factory pattern. Need help!!

Similar Threads

  1. [mac] Pattern Finder - Help needed
    By Tanaris4 in forum WoW Memory Editing
    Replies: 9
    Last Post: 09-16-2009, 03:27 PM
  2. need help with shammy talents
    By jason in forum World of Warcraft General
    Replies: 5
    Last Post: 07-19-2006, 02:02 AM
  3. I need help
    By ff9pro in forum World of Warcraft General
    Replies: 4
    Last Post: 07-05-2006, 08:43 PM
  4. Need help
    By zamp in forum World of Warcraft General
    Replies: 1
    Last Post: 06-24-2006, 10:54 PM
  5. Need Help
    By Slumlorde in forum World of Warcraft General
    Replies: 4
    Last Post: 06-23-2006, 08:20 AM
All times are GMT -5. The time now is 01:16 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search