Friday, January 17, 2014

Decorator/Wrapper Design Pattern

The Decorator/Wrapper pattern is used to add more functions to an existing class or interface. For example, you need to sort an array of Vectors, you have to make the vectors comparable. This can be done in two ways, one is using a subclass of Vector, the other is using the Decorator pattern.

1. Using a subclass


//Can access Vector method
public class ComparableVector extends Vector implements Comparable {
      public int compareTo (Object obj) {
            ComparableVector toBeCompared = (ComparableVector) obj;

            if (this.get(0) > toBeCompared.get(0)) {
                  return 1;
            } else if (this.get(0) < toBeCompared.get(0)) {
                  return -1;
            } else {
                  return 0;
            }
      }
}

public class VectorSorter1 {
      public static void main (String[] args) {
            ComparableVector cv1 = new ComparableVector(Arrays.asList(new Integer(100), newInteger(80), new Integer(101));

            ComparableVector cv2 =  new ComparableVector(Arrays.asList(new Integer(10), newInteger(88));

            ComparableVector cv3 = new ComparableVector(Arrays.asList(new Integer(130), newInteger(400), new Integer(10), new Integer(10));

            ComparableVector[] array = {cv1 ,cv2, cv3);

            Arrays.sort(array);
      }
}

2. Using the Decorator/Wrapper pattern


//Cannot directly access Vector methods.
public class ComparableVector implements Comparable {
     private Vector vector;

      public ComparableVector(Vector v) {
            vector = v;
      }

      public Vector getVector() {
            return vector;
      }

     //Need to write a method to access the corresponding Vector method
      public int size() {
            return vector.size();
      }

      public int compareTo (Object obj) {
            ComparableVector cv = (ComparableVector) obj;
            Vector toBeCompared = cv.getVector();

            Integer v1 = this.getVector().get(0);
            Integer v2 = toBeCompared.get(0);

            if (v1 > v2) {
                  return 1;
            } else if (v1 < v2) {
                  return -1;
            } else {
                  return 0;
            }
      }
}

public class VectorSorter2 {
      public static void main (String[] args) {
            Vector v1 = new Vector(Arrays.asList(new Integer(100), newInteger(80), new Integer(101));
             ComparableVector cv1 = new ComparableVector(v1);

            Vector v2 =  new Vector(Arrays.asList(new Integer(10), newInteger(88));
            ComparableVector cv2 = new ComparableVector(v2);

            Vector v3 = new Vector(Arrays.asList(new Integer(130), newInteger(400), new Integer(10), new Integer(10));
            ComparableVector cv3 = new ComparableVector(v3);

            ComparableVector[] array = {cv1 ,cv2, cv3);

            Arrays.sort(array);
      }
}

----------------------------------------------------------------------------------------------------------------
      
              
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment