Archive for May, 2007

 

Design Patterns – Observer pattern (Behavioral) basic example..

May 25, 2007 in Engineering, Software

This is a (simplified) example from Vince Huston’s patterns pages. The part I found really neat is the self-registration methodology of observers.. normally, I expected the Subject to have an ‘add()’ method for observers.. this is a bit different.. nice..

// ObserverEx.java   

// From Vince Huston's site..

class ObserverEx {
    public static void main(String[] args) {
        Subject subj = new Subject();
        // Self-Register observers.. nice..
        new SomeObserver( subj );
        new OtherObserver( subj );
        // Do something to the subject..
    }
}

class Subject {
    private Observer[] observers = new Observer[9];
    private int        numObs  = 0;

    public void attach( Observer o ) {
        observers[numObs++] = o;
    }

    private void notifyObservers() {
        for (int ix = 0; ix < numObs; ix++) {
            observers[ix].update();
        }
    }

}

abstract class Observer {
    protected Subject subj;
    public abstract void update();
    public Observer( Subject subj ) {
        this.subj = subj;
    }
}

class SomeObserver extends Observer {
    public SomeObserver( Subject s ) {
        super( s );
        subj.attach(this);
    }

    public void update() {
        // Update from Subject..
    }
}

class OtherObserver extends Observer {
    public OtherObserver( Subject s ) {
        super( s );
        subj.attach(this);
    }

    public void update() {
        // Update from Subject..
    }
}


Eclipse TPTP Setup with Tomcat ..

May 10, 2007 in Eclipse, Java, Uncategorized

Good info on setting up Eclipse TPTP (Tracing and Profiling Tools Project) with Tomcat.

http://www.symentis.com/w-howTo/Eclipse-TPTP-Tomcat-HowTo.pdf

tptp

Design Patterns – Strategy pattern (Behavioral) basic example..

May 08, 2007 in Engineering, Software

A possible usage scenario without thinking ‘Strategy’ ..


// Example without using the Strategy pattern

public class WithOutStrategy { 

    public void doSomething(int theSwitch) { 

        if ( theSwitch == 0 ) { 
            AClass ac = new AClass(); 
            ac.doit(); 
        } 

        if ( theSwitch == 1 ) { 
            BClass bc = new BClass(); 
            bc.doit(); 
        } 

    } 

    public static void main(String[] args) { 

        WithOutStrategy sb = new WithOutStrategy(); 
        sb.doSomething(0); 
        sb.doSomething(1); 
    } 

} 

class AClass { 

    public void doit() { 
        System.out.println(">> doit A"); 
    } 

} 

class BClass { 

    public void doit() { 
        System.out.println(">> doit B"); 
    } 

}

And a better way of doing it with ‘Strategy’..


// Example using Strategy

public class WithStrategy { 

    public void doSomething(OClass oc) { 
        oc.doit(); 
    } 

    public static void main(String[] args) { 

        WithStrategy ows = new WithStrategy(); 
        OClass oc = null; 

        oc = new AClass(); 
        ows.doSomething(oc); 

        oc = new BClass(); 
        ows.doSomething(oc); 
    } 

} 

// Options class 

abstract class OClass { 
    public abstract void doit(); 
} 

class AClass extends OClass { 

    public void doit() { 
        System.out.println(">> doit A"); 
    } 

} 

class BClass extends OClass { 

    public void doit() { 
        System.out.println(">> doit B"); 
    } 

}