Pages

Wednesday, May 6, 2015

Adapter Pattern example

Adapter Pattern

It helps to assign/convert one interface (called as source) to another distinct interface (called as target).

Example Description:

The below code helps us to use legacy Enumeration class available in Vector class in place of modern Iterator class.

package designpattern;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author DELL
 */
public class AdapterPatternExample {

    public static void main(String[] args) {
        demo();
    }

    public static void demo() {
        Vector namelist = new Vector();
        namelist.add("Athiruban");
        namelist.add("Athinivas");
        Enumeration legacyIterator = namelist.elements();
        Iterator iterator;

        LegacyIteratorAdapter adapter = new LegacyIteratorAdapter(legacyIterator);
        //iterator = legacyIterator;    /* this code will fail */
        iterator = adapter;
        for (; iterator.hasNext();) {
            System.out.println(iterator.next().toString());
        }
    }
}

class LegacyIteratorAdapter implements Iterator {
    /*
     * The purpose of the class to adapt the legacy Enumeration interface to
     * modern Iterator interface.
     */

    Enumeration legacyIterator;

    public LegacyIteratorAdapter(Enumeration e) {
        legacyIterator = e;
    }

    @Override
    public boolean hasNext() {
        return legacyIterator.hasMoreElements();
    }

    @Override
    public Object next() {
        return legacyIterator.nextElement();
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

Saturday, May 2, 2015

Coding refresher on Github

It gives an immense pleasure to share my technical experience with fellow people. I started pushing all my local code fragments/programs to github as a way of helping young/new programmers on how to code in Java.

Github link to my program list that helped me to attain "Oracle Certified Java Professional".

https://github.com/athiruban/CodingRefresher

Many new interesting topics on DataStructure and Algorithms will be added to the above repo.

Please visit and share your comments on how we can improve this further.

Friday, April 3, 2015

Largest rectangle in a histogram

The below code finds the rectangle with maximum area in a histogram with O(n) time complexity.

    public static void maxRectArea(int a[], int b[]) {
        // this is a maximization problem
        // irrespective of the height of the the next bar
        // we have to compare the area if including next bar exceeds the current area then we have to include the bar.
        // stop the expansion and proceed further until end of input.
        // 6 2 5 4 5  1 6
        // 6 2 5 8 12 1 6
        int i = 0;
        int max = -1;
        int e = 0;

        while (i < a.length) {
            if (i == 0)
                e = b[i] = a[i];
            else {
                if (a[i] < a[i-1]) {
                    int t = b[i-1] / e;
                    if (((t+1) * a[i]) < b[i-1])
                        e = b[i] = a[i];
                    else {
                        e = a[i];
                        b[i] = (t+1) * a[i];    
                    }
                }
                else if (a[i] == a[i-1]) {
                    b[i] = b[i-1] + e;
                }
                else if (a[i] > a[i-1]) {
                    if ((b[i-1] + e) > a[i]) {
                        b[i] = b[i-1] + e;
                    }
                    else {
                        e = b[i] = a[i];
                    }
                }         
            }
            i++;
        }
    }