Pages

Showing posts with label Design Pattern. Show all posts
Showing posts with label Design Pattern. Show all posts

Thursday, May 7, 2015

Observer Pattern example



package designpatterns;

/*
 * Observer Pattern example
 */

class WeatherData {

    float temperature;
    float humidity;
    
    public float getTemperature(){
        return temperature;
    }
    
    public float getHumidity() {
        return humidity;
    }
    
    public void setTemperature(float t) {
        temperature = t;
    }
    
    public void setHumidity(float h) {
        humidity = h;
    }
}

/*
 * The below interface adds functionality to add, remove subscribers/clients 
 *  to the internal list so that they can be notified whenever the state of
 *  the weather changes.
 */
 
interface WeatherNotifier {
    public void notify();
    public void addListener(Object client);
    public void removeListener(Object client);
}

/*
 * WeatherData after implementing the above interface
 */

class WeatherData implements WeatherNotifier {
    private List subscribers;
    {
        subscribers = new ArrayList();
    }
    
    public void notify() {
        // notify/wake up all the subscribers to update their view with latest state information
        Iterator iterator = subscribers.iterator();
        while (iterator.hasNext()) {
            iterator.next().update();
        }
    }

    @Override    
    public void addListener(Object client) {
        subscribers.add(client);
    }
    
    @Override
    public void removeListener(Object client) {
        subscribers.remove(client);
    }

   public void setTemperature(float t) {
        temperature = t;
        dataChanged();
    }
    
    public void setHumidity(float h) {
        humidity = h;
        dataChanged();
    }
    
    private void dataChanged() {
        notify();
    }

    public float getTemperature(){
        return temperature;
    }
    
    public float getHumidity() {
        return humidity;
    }

}

interface WeatherReader {
    public void update();
}

class WeatherClient implements WeatherReader{
    WeatherData source;
    
    public void update() {
        System.out.println("Temperature is " + source.getTemperature());
        System.out.println("Humidity is    " + source.getHumidity());       
    }
}

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.
    }
}